a = re.match('test','testasdtest1212131212')if a isnotNone:print(a)# 返回一个匹配对象print(a.group())# 返回test,获取不到则报错print(a.span())else:print("未匹配到")
输出结果是:<re.Match object; span=(0,4),match='test'># 返回的匹配对象
test # 返回匹配结果,如果没有获取到就报错(0,4)# 表示字符串下标
\d匹配
# 从开头进行匹配,\d\d,表示字符串前两位是否位数字
a = re.match('\d\d','12testasdtest1212131212')if a isnotNone:print(a)# 返回一个匹配对象print(a.group())# 返回test,获取不到则报错print(a.span())else:print("未匹配到")
\D 匹配非数字匹配
a = re.match('\D\D','testasdtest1212131212')if a isnotNone:print(a)# 返回一个匹配对象print(a.group())# 返回test,获取不到则报错print(a.span())else:print("未匹配到")
输出结果是:
<re.Match object; span=(0,2),match='te'>
te
(0,2)
\s 匹配特殊字符,如空白,空格,tab等
判断是否由特殊字符开头
a = re.match('\s\s',' testasdtest1212131212')if a isnotNone:print(a)# 返回一个匹配对象print(a.group())# 返回test,获取不到则报错print(a.span())else:print("未匹配到")
输出结果是:
<re.Match object; span=(0,2),match=' '>(0,2)
\S 匹配非空白
# 匹配开头为非空
a = re.match('\S','testasdtest1212131212')if a isnotNone:print(a)# 返回一个匹配对象print(a.group())# 返回test,获取不到则报错print(a.span())else:print("未匹配到")
输出结果是:
<re.Match object; span=(0,1),match='t'>
t
(0,1)
[ ] 匹配[ ]中列举的字符
a = re.match('23[abcde]','23es 12testasdtest')# 如果前两个匹配都是失败的,就之间报错if a isnotNone:print(a)# 返回一个匹配对象print(a.group())# 返回test,获取不到则报错print(a.span())else:print("未匹配到")
不匹配中间的某个字符开头
a = re.match('12[^12346789]','1252s12testasdtest')# 如果是第三位是[^]中的数据就是没匹配到,如果不是就成功匹配if a isnotNone:print(a)# 返回一个匹配对象print(a.group())# 返回test,获取不到则报错print(a.span())else:print("未匹配到")
输出结构是:<re.Match object; span=(0,3),match='125'>125(0,3)
[a-z3-5] 匹配a-z或者3-5中的字符
a = re.match('12[1-3a-c]','1232b12testasdtest')# 第三位如果是[]中的数据就正常匹配,否者为空if a isnotNone:print(a)# 返回一个匹配对象print(a.group())# 返回test,获取不到则报错print(a.span())else:print("未匹配到")
输出结果是:<re.Match object; span=(0,3),match='123'>123(0,3)
a = re.match('\d*','1232b12testasdtest')# 检索前面数字所有的if a isnotNone:print(a)# 返回一个匹配对象print(a.group())# 返回test,获取不到则报错print(a.span())else:print("未匹配到")
输出结果是:<re.Match object; span=(0,4),match='1232'>1232(0,4)
+ 至少出现一次
a = re.match('a+','aaatestasdtest')# 前面a就会匹配到,要是不是a就是报错if a isnotNone:print(a)# 返回一个匹配对象print(a.group())# 返回test,获取不到则报错print(a.span())else:print("未匹配到")
输出结果是:
<re.Match object; span=(0,3),match='aaa'>
aaa
(0,3)
? 1次或则0次
a = re.match('a?','testasdtest')if a isnotNone:print(a)# 返回一个匹配对象print(a.group())# 返回test,获取不到则报错print(a.span())else:print("未匹配到")
输出结果是:
<re.Match object; span=(0,0),match=''>(0,0)
{m}指定出现m次
a = re.match('to{3}','tooabatestasdtest')if a isnotNone:print(a)# 返回一个匹配对象print(a.group())# 返回test,获取不到则报错print(a.span())else:print("未匹配到")
输出结果是:
未匹配到
a = re.match('to{3}','toooabatestasdtest')if a isnotNone:print(a)# 返回一个匹配对象print(a.group())# 返回test,获取不到则报错print(a.span())else:print("未匹配到")
输出结果是:
<re.Match object; span=(0,4),match='tooo'>
tooo
(0,4)
{m,} 至少出现m次
a = re.match('to{3,}','toooooooabatestasdtest')if a isnotNone:print(a)# 返回一个匹配对象print(a.group())# 返回test,获取不到则报错print(a.span())else:print("未匹配到")
输出结果是:
<re.Match object; span=(0,8),match='tooooooo'>
tooooooo
(0,8)
{m,n} 指定从m-n次的范围
a = re.match('to{3,4}','toooooooabatestasdtest')if a isnotNone:print(a)# 返回一个匹配对象print(a.group())# 返回test,获取不到则报错print(a.span())else:print("未匹配到")
输出结果是:
<re.Match object; span=(0,5),match='toooo'>
toooo
(0,5)
$ 匹配结尾字符
a = re.match('to{3,4}.*t$','toooooooabatestasdtest')if a isnotNone:print(a)# 返回一个匹配对象print(a.group())# 返回test,获取不到则报错print(a.span())else:print("未匹配到")
输出结果是:
<re.Match object; span=(0,22),match='toooooooabatestasdtest'>
toooooooabatestasdtest
(0,22)
^ 匹配开头字符
a = re.match('^t','toooooooabatestasdtest')if a isnotNone:print(a)# 返回一个匹配对象print(a.group())# 返回test,获取不到则报错print(a.span())else:print("未匹配到")
输出结果是:
<re.Match object; span=(0,1),match='t'>
t
(0,1)
\b 匹配一个单词的边界
\b:表示字母数字与非字母数字的边界,非字母数字与字母数字的边界。即下面ve的右边不能有字母和数字
a = re.match(r'.*ve\b','121ve.2testable')#因为在python中\代表转义,所以前面加上r消除转义if a isnotNone:print(a)# 返回一个匹配对象print(a.group())# 返回test,获取不到则报错print(a.span())else:print("未匹配到")
输出结果是:
<re.Match object; span=(0,5),match='121ve'>
121ve
(0,5)
\B 匹配非单词边界
a = re.match(r'.*bl\B','121ve.2testable')if a isnotNone:print(a)# 返回一个匹配对象print(a.group())# 返回test,获取不到则报错print(a.span())else:print("未匹配到")
输出结果是:
<re.Match object; span=(0,14),match='121ve.2testabl'>
121ve.2testabl
(0,14)
| 匹配左右任意一个表达式
a = re.match(r'\d[1-9]|\D[a-z]','12asdasdas')if a isnotNone:print(a)# 返回一个匹配对象print(a.group())# 返回test,获取不到则报错print(a.span())else:print("未匹配到")
(ab) 将括号中字符作为一个分组
a = re.match(r'<h1>(.*)<h1>','<h1>你好啊<h1>')if a isnotNone:print(a)# 返回一个匹配对象print(a.group())# 返回test,获取不到则报错print(a.span())else:print("未匹配到")
输出结果是:
<re.Match object; span=(0,11),match='<h1>你好啊<h1>'><h1>你好啊<h1>(0,11)
search从字符串中搜索
和match用法差不多,这个是从字符串中进行搜索
import re
a = re.search(r'asasa','asfakjfaasasadaslkdnas>')if a isnotNone:print(a)# 返回一个匹配对象print(a.group())# 返回test,获取不到则报错print(a.span())else:print("未匹配到")
输出结果是:
<re.Match object; span=(8,13),match='asasa'>
asasa
(8,13)
findall使用
字面意思来看可以看到,findall是寻找所有能匹配到的字符,把并已列表的方式返回
import re
a = re.findall(r'1[0-9]\d*','asfakjfaa1534479947sasadaslkdnas>')if a isnotNoneandtype(a)!=list:print(a)# 返回一个匹配对象print(a.group())# 返回test,获取不到则报错print(a.span())eliftype(a)islistand a isnot[]:print(a)else:print("未匹配到")
输出结果是:
['1534283479947']
re.s
findall中另外一个属性re.S
在字符串a中,包含换行符\n,在这种情况下
.如果不适用re.S参数,则只在每一行内进行匹配,如果一行没有就换下一行重新开始
.而使用re.S参数一会,正则表达式会将这个字符串作为一个整体,在整体中进行匹配
b ="""aaatestaa
aaaa123"""
a = re.findall(r'1[0-9]\d*', b, re.S)if a isnotNoneandtype(a)!=list:print(a)# 返回一个匹配对象print(a.group())# 返回test,获取不到则报错print(a.span())eliftype(a)islistand a isnot[]:print(a)else:print("未匹配到")
输出结果是:['123']
import re
s ="itcase,java:php-ph:p3;html"print(re.split(r",", s))# 以,号进行分割print(re.split(r",|:|-|;", s))# 以,或者:或者-或者;进行分割print(re.split(r",|:|-|%", s))# 找不到的分隔符就忽略
输出结果是:
['itcase','java:php-ph:p3;html']['itcase','java','php','ph','p3','html']['itcase','java','php','ph','p3;html']
贪婪与非贪婪
python里的数量词默认是贪婪的,总是尝试尽可能的匹配更多的字符,python中使用?号关闭贪婪模式
例子
print(re.match(r"aa\d+","aa2323"))#会尽可能多的去匹配\dprint(re.match(r"aa\d+?","aa2323"))#尽可能少的去匹配\d
输出结果是:
<re.Match object; span=(0,6),match='aa2323'><re.Match object; span=(0,3),match='aa2'>
例子二
import re
s ="this is a number 234-235-22-423"# 1.贪婪模式
resule = re.match(r"(.+)(\d+-\d+-\d+-\d)",s)#我们本想数字和字母拆解成两个分组print(resule.groups())#('this is a number 23', '4-235-22-4')但我们发现输出的结果中23的数字竟然被弄到前面去了#因为+它会尽可能多的进行匹配,\d,只需要一个4就能满足,所以前面就尽可能多的匹配# 2.关闭贪婪模式#在数量词后面加上 ?,进入非贪婪模式,尽可能少的进行匹配
result = re.match(r"(.+?)(\d+-\d+-\d+-\d)",s)print(result.groups())#('this is a number ', '234-235-22-4')
解释:
是一个正则表达式函数,用于在字符串s的开头进行匹配。它的作用是检查字符串是否以一个或多个非空字符开头,后面跟着一个日期格式(形如YYYY-MM-DD-HH)的子串。
如果匹配成功,re.match()函数会返回一个匹配对象,否则返回None。这个正则表达式使用了两个括号捕获组,第一个捕获组(.+)匹配一个或多个任意字符,第二个捕获组(\d+-\d+-\d+-d)匹配日期格式
import re
str="http://10.1.1.1/index.html"try:
s = re.findall("\d*?\.\d*?\.\d*?\.\d*",str)# print(s.group())print(s)except:print("未能匹配出ip地址")
输出结果是:
['10.1.1.1']
从响应包里提取关键词所在的行,可以在正则中添加变量
import re
key ="ID"
a ="""
<body data-spm="7663354">
<div data-spm="1998410538">
<div class="header">
<div class="container">
<div class="message">
很抱歉,由于您访问的URL有可能对网站造成安全威胁,您的访问被阻断。
<div>您的请求ID是: <strong>
781bad0a16702307419116917e43b3</strong></div>
</div>
</div>
</div>
"""
res = re.search(r'<.*>(.*?%s.*?)<.*?>'%(key),a,re.S)print(res.group(1).replace("\n","").replace(" ",""))
输出结果是:
您的请求ID是: