day正则表达式补充

news/2024/7/2 1:21:23

# 2.正则


# 方法:findall | match | split | sub
# a = 10
# print(a.__hash__())
# def fn():
# pass
# print(fn.__name__)
# import json
# print(json.dumps([1,2,3]))
import re


# 全文匹配,返回值是列表
res = re.findall('\d*?', 'd1') # ['', '', ''] | ['', '', '1', '']
print(res)


# 非贪婪匹配的引用场景: 一般都要结合特定的开头与结尾
res = re.findall('<.*>', '<a>abc</a>')
print(res) # ['<a>abc</a>']
res = re.findall('<.*?>', '<a>abc</a>')
print(res) # ['<a>', '</a>']
res = re.findall('\w*?>', '<a>abc</a>')
print(res) # ['a>', 'a>']

 

# 分组:通过分组加(),拿到匹配到的结果中的指定信息
res = re.findall('((\w*?)>)', '<a>abc</a>')
print(res) # [('a>', 'a'), ('a>', 'a')]

 

# 操作分组的方法
# (?P<name>...): 有名分组
# 返回值是match匹配的结果对象,可以.group(组序号|组名)来取具体组的信息
res = re.match('(\d{3})(?P<center>\d{3})(\d{3})', '123456789')
print(res.group('center'))

 

# 传入一个有正则语法的普通字符串,得到一个可以调用正则方法的正则字符串
r = re.compile('(\w*?)>')
print(r.findall('<a>abc</a>'))

# res = re.findall('<([a-z]{1,3})>(\w*?)</[a-z]{1,3}>', '<a>abc</a><b>123</b>')
# print(res) # [('a', 'abc'), ('b', '123')]

res = re.sub('(\d{3})(?P<center>\d{3})(\d{3})', r'\2\1\3', '<123456789>')
print(res)

 

#

[0-9]  #匹配数字0~9
[a-z]  #匹配小写字母
[A-Z]  #匹配大写字母
\d | \D #匹配数字 | 非数字
\w| \W       #匹配字母数字下划线 | 非字母数字下划线
\s | \S      #匹配不可见字符:\n \t 空格等 | 可见字符
\b:边界匹配, 按空格结束匹配  需要用r将正则转义,不然得不到想要的结果
.

a|b == [ab]
[^msg]: msg的对立面

*
+
?

*?
+?
??
'''
import re
print(re.findall(r'a[a-z]*\b', 'a ab c abc def ab'))
print(re.findall(r'[^ab]', ' aab c abc def ab'))

# 身份证:18位
# 200000200808081111
# [1-7][0-9]{5}
# (?:19[0-9]{2}|20[01][0-9]): 1900 - 2019
# (?:0[1-9]|1[0-2]): 01-12
# (?:0[1-9]|[12][0-9]|3[01]) : 01-31
# [0-9]{3}
# [0-9Xx]
# [1-7][0-9]{5}(?:19[0-9]{2}|20[01][0-9])(?:0[1-9]|1[0-2])(?:0[1-9]|[12][0-9]|3[01])[0-9]{3}[0-9Xx]

# 邮箱
# 30000000@qq.com
# [a-zA-Z0-9]\w{,15}@[176|178|192]\.[com|com\.cn|cn]

 

# 获取百度首页图片地址
import requests
responds = requests.get('https://www.baidu.com/')
# print(responds.text)
content = responds.text
# www.baidu.com/img/gs.gif
content += 'http://www.baidu.com/wimg/gs.gif'
# res_list = re.findall('www[^w]*?(?:gif|png)', content)
res_list = re.findall('www.{1,30}(?:gif|png)', content)
# print(len(res_list))
print(res_list)

转载于:https://www.cnblogs.com/maoruqiang/p/10723722.html


http://www.niftyadmin.cn/n/4820381.html

相关文章

网络、HTTP相关学习总结

引言&#xff1a;上图是《图解 HTTP》这本书的目录&#xff0c;最近又重新读了一下这本书&#xff0c;然后打算写篇总结&#xff0c;来加深自己的理解&#xff0c;然而在做总结的时候觉得还是全面总结下网络相关的知识吧&#xff0c;所以又在网上找了大量的资料&#xff0c;最终…

sawfish-lisp-source_1.3.1-1_all.deb

sawfish-lisp-source_1.3.1-1_all.deb 的下载页面 如果您正在运行 Ubuntu&#xff0c;请尽量使用像 aptitude 或者 synaptic 一样的软件包管理器&#xff0c;代替人工手动操作的方式从这个网页下载并安装软件包。 您可以使用以下列表中的任何一个源镜像只要往您的 /etc/apt/sou…

TCL解释器与C++代码交互过程?

工 具 心 情 休 闲 博 客 论 坛 游客: 注册 | 登录 | 搜索 | 繁體中文 百思论坛 网络仿真软件 NS TCL解释器与C代码交互过程&#xff1f;上一主题下一主题 可打印版本 | 订阅主题 | 收藏主题 | 开通个人空间 <script type"text/javascript"> fun…

leetcode_654. Maximum Binary Tree

https://leetcode.com/problems/maximum-binary-tree/ 给定数组A&#xff0c;假设A[i]为数组最大值&#xff0c;创建根节点将其值赋为A[i]&#xff0c;然后递归地用A[0,i-1]创建左子树&#xff0c;用A[i1&#xff0c;n]创建右子树。 使用vector的assign函数,该函数的特性&#…

【vueJs源码】阅读之vm.$watch函数

我们经常使用watch肯定知道它&#xff0c;他和computer一样都是数据发生变化都会触发它。今天我们就来了解一下它的原理。 他的用法 Vue.prototype.$watch function (expOrFn: string | (() > any),cb: any,options?: Record<string, any> ): Function这是vuejs源…

Common Lisp

Common Lisp 来自 维客 Jump to: navigation, searchCommon Lisp&#xff0c;一般缩写为 CL&#xff08;不要和缩写同为CL的組合邏輯混淆&#xff09;&#xff0c;是Lisp的方言&#xff0c;标准由ANSI X3.226-1994定义。它是为了标准化此前众多的Lisp分支而开发的&#xff0c;它…

qa qc qm的区别

稍后更新。。。转载于:https://www.cnblogs.com/Chamberlain/p/10586562.html

python --web服务器

用python建立简单的web服务器 利用Python自带的包可以建立简单的web服务器。 在DOS里cd到准备做服务器根目录的路径下&#xff0c;输入命令&#xff1a; python -m Web服务器模块 [端口号&#xff0c;默认8000] 例如&#xff1a; python -m SimpleHTTPServer 8080 然后就…