潍坊网站建设一品网络小程序,数据分析培训课程,wordpress手机版受钱吗,企业网站域名空间在Python中需要通过正则表达式对字符串进⾏匹配的时候#xff0c;可以使⽤⼀个python自带的模块#xff0c;名字为re。
正则表达式的大致匹配过程是#xff1a; 1.依次拿出表达式和文本中的字符比较#xff0c; 2.如果每一个字符都能匹配#xff0c;则匹配成功#xff1…在Python中需要通过正则表达式对字符串进⾏匹配的时候可以使⽤⼀个python自带的模块名字为re。
正则表达式的大致匹配过程是 1.依次拿出表达式和文本中的字符比较 2.如果每一个字符都能匹配则匹配成功一旦有匹配不成功的字符则匹配失败。 3.如果表达式中有量词或边界这个过程会稍微有一些不同。
rPython 中字符串的前导 r 代表原始字符串标识符该字符串中的特殊符号不会被转义适用于正则表达式中繁杂的特殊符号表示。 因此 r\n 表示包含 \ 和 n 两个字符的字符串而 \n 则表示只包含一个换行符的字符串。
print(\\n) # 输出 \n
print(r\n) #输出 \n
re模块的使用import re
re.match函数
语法re.match(pattern, string, flags0)
pattern匹配的正则表达式string要匹配的字符串flags 标志位用于控制正则表达式的匹配方式如是否区分大小写多行匹配等等。 re.I 忽略大小写re.L 表示特殊字符集 \w, \W, \b, \B, \s, \S 依赖于当前环境re.M 多行模式re.S 即为 . 并且包括换行符在内的任意字符. 不包括换行符re.U 表示特殊字符集 \w, \W, \b, \B, \d, \D, \s, \S 依赖于 Unicode 字符属性数据库re.X 为了增加可读性忽略空格和 # 后面的注释
尝试从字符串的起始位置匹配一个模式如果不是起始位置匹配成功的话match()就返回none。匹配成功re.match方法返回一个匹配的对象。
如果上⼀步匹配到数据的话可以使⽤group⽅法来提取数据。以使用group(num) 或 groups() 匹配对象函数来获取匹配表达式。
group()用来提出分组截获的字符串用来分组group() 同group0就是匹配正则表达式整体结果group(1) 列出第一个括号匹配部分group(2) 列出第二个括号匹配部分group(3) 列出第三个括号匹配部分。没有匹配成功的re.search()返回None。
举例 import reresult re.match(itcast,itcast.cn)result.group()
itcast
从string头开始匹配pattern完全可以匹配pattern匹配结束同时匹配终止后面的.cn不再匹配返回匹配成功的信息。
匹配单个字符
字符功能位置.匹配任意1个字符除了\n[ ]匹配[ ]中列举的字符\d匹配数字即0-9可以写在字符集[...]中\D匹配⾮数字即不是数字可以写在字符集[...]中\s匹配空⽩即空格tab键可以写在字符集[...]中\S匹配⾮空⽩字符可以写在字符集[...]中\w匹配单词字符即a-z、A-Z、0-9、_可以写在字符集[...]中\W匹配⾮单词字符可以写在字符集[...]中\w\w 匹配单词字符即a-z、A-Z、0-9、_\W匹配⾮单词字符
[...]字符集对应的位置可以是字符集中任意字符。字符集中的字符可以逐个列出也可以给出范围比如[abc]和[a-c]第一个字符如果是^表示取反。所有特殊字符比如]-^在字符集中都失去原来的含义如要使用可把]-放在第一个字符^放在非第一个字符。
举例
import re
ret re.match(.,M)
print(ret.group())
ret re.match(t.o,too)
print(ret.group())
ret re.match(t.o,two)
print(ret.group())
# 如果hello的⾸字符⼩写那么正则表达式需要⼩写的h
ret re.match(h,hello Python)
print(ret.group())
# 如果hello的⾸字符⼤写那么正则表达式需要⼤写的H
ret re.match(H,Hello Python)
print(ret.group())
# ⼤⼩写h都可以的情况
ret re.match([hH],hello Python)
print(ret.group())
ret re.match([hH],Hello Python)
print(ret.group())
ret re.match([hH]ello Python,Hello Python)
print(ret.group())
# 匹配0到9的多种写法
ret re.match([0123456789]Hello Python,7Hello Python)
print(ret.group())
ret re.match([0-9]Hello Python,7Hello Python)
print(ret.group())
# 匹配0到3和5-9
ret re.match([0-35-9]Hello Python,7Hello Python)
print(ret.group())
ret re.match([0-35-9]Hello Python,4Hello Python)
#print(ret.group())
ret re.match(嫦娥\d号,嫦娥1号发射成功)
print(ret.group())
ret re.match(嫦娥\d号,嫦娥2号发射成功)
print(ret.group())
结果
M too two h H h H Hello Python 7Hello Python 7Hello Python 7Hello Python 嫦娥1号 嫦娥2号
匹配多个字符
字符功能位置表达式实例完整匹配的字符串*匹配前⼀个字符出现0次或者⽆限次即可有可⽆用在字符或(...)之后abc*abccc匹配前⼀个字符出现1次或者⽆限次即⾄少有1次用在字符或(...)之后abcabccc?匹配前⼀个字符出现1次或者0次即要么有1次要么没有用在字符或(...)之后abc?ab,abc{m}匹配前⼀个字符出现m次用在字符或(...)之后ab{2}cabbc{m,n}匹配前⼀个字符出现从m到n次若省略m则匹配0到n次若省略n则匹配m到无限次用在字符或(...)之后ab{1,2}cabc,abbc
举例
import re
#匹配出⼀个字符串第⼀个字⺟为⼤写字符后⾯都是⼩写字⺟并且这些⼩写字⺟可有可⽆
ret re.match([A-Z][a-z]*,M)
print(ret.group())
ret re.match([A-Z][a-z]*,MnnM)
print(ret.group())
ret re.match([A-Z][a-z]*,Aabcdef)
print(ret.group())
#匹配出变量名是否有效
names [name1, _name, 2_name, __name__]
for name in names:ret re.match([a-zA-Z_][\w]*,name)if ret:print(变量名 %s 符合要求 % ret.group())else:print(变量名 %s ⾮法 % name)
#匹配出0到99之间的数字
ret re.match([1-9]?[0-9],7)
print(ret.group())
ret re.match([1-9]?\d,33)
print(ret.group())
# 这个结果并不是想要的利⽤$才能解决
ret re.match([1-9]?\d,09)
print(ret.group())
ret re.match([a-zA-Z0-9_]{6},12a3g45678)
print(ret.group())
#匹配出8到20位的密码可以是⼤⼩写英⽂字⺟、数字、下划线
ret re.match([a-zA-Z0-9_]{8,20},1ad12f23s34455ff66)
print(ret.group())
结果 M Mnn Aabcdef 变量名 name1 符合要求 变量名 _name 符合要求 变量名 2_name ⾮法 变量名 __name__ 符合要求 7 33 0 12a3g4 1ad12f23s34455ff66
匹配开头结尾
字符功能^匹配字符串开头$匹配字符串结尾
举例匹配163.com的邮箱地址
import re
email_list [xiaoWang163.com, xiaoWang163.comheihei, .com.xiaowangqq.com]
for email in email_list:ret re.match([\w]{4,20}163\.com$, email)if ret:print(%s 是符合规定的邮件地址,匹配后的结果是:%s % (email, ret.group()))else:print(%s 不符合要求 % email)
结果
xiaoWang163.com 是符合规定的邮件地址,匹配后的结果是:xiaoWang163.com xiaoWang163.comheihei 不符合要求 .com.xiaowangqq.com 不符合要求
匹配分组
字符功能|匹配左右任意⼀个表达式(ab)将括号中字符作为⼀个分组\num引⽤分组num匹配到的字符串(?Pname)分组起别名匹配到的子串组在外部是通过定义的 name 来获取的(?Pname)引⽤别名为name分组匹配到的字符串
举例|
#匹配出0-100之间的数字
import re
ret re.match([1-9]?\d$|100,8)
print(ret.group()) # 8
ret re.match([1-9]?\d$|100,78)
print(ret.group()) # 78
ret re.match([1-9]?\d$|100,08)
# print(ret.group()) # 不是0-100之间
ret re.match([1-9]?\d$|100,100)
print(ret.group()) # 100
举例()
#需求匹配出163、126、qq邮箱
ret re.match(\w{4,20}163\.com, test163.com)
print(ret.group()) # test163.com
ret re.match(\w{4,20}(163|126|qq)\.com, test126.com)
print(ret.group()) # test126.com
ret re.match(\w{4,20}(163|126|qq)\.com, testqq.com)
print(ret.group()) # testqq.com
ret re.match(\w{4,20}(163|126|qq)\.com, testgmail.com)
if ret:print(ret.group())
else:print(不是163、126、qq邮箱) # 不是163、126、qq邮箱
#不是以4、7结尾的⼿机号码(11位)
tels [13100001234, 18912344321, 10086, 18800007777]
for tel in tels:ret re.match(1\d{9}[0-35-68-9], tel)if ret:print(ret.group())else:print(%s 不是想要的⼿机号 % tel)
#提取区号和电话号码
ret re.match(([^-]*)-(\d),010-12345678)
print(ret.group())
print(ret.group(1))
print(ret.group(2))
举例\number
匹配数字代表的组合。每个括号是一个组合组合从1开始编号。比如 (.) \1 匹配 the the 或者 55 55, 但不会匹配 thethe (注意组合后面的空格)。这个特殊序列只能用于匹配前面99个组合。如果 number 的第一个数位是0 或者 number 是三个八进制数它将不会被看作是一个组合而是八进制的数字值。在 [ 和 ] 字符集合内任何数字转义都被看作是字符。
例子1匹配出 htmlhh/html
\1,...,\9匹配第n个分组的内容。如例子所示指匹配第一个分组的内容。
import re
# 正确的理解思路如果在第⼀对中是什么按理说在后⾯的那对中就应该是什么。通过引⽤分组中匹配到的数据即可但是要注意是元字符串即类似 r这种格式。
ret re.match(r([a-zA-Z]*)\w*/\1, htmlhh/html)
# 因为2对中的数据不⼀致所以没有匹配出来
test_label [htmlhh/html,htmlhh/htmlbalabala]
for label in test_label:ret re.match(r([a-zA-Z]*)\w*/\1, label)if ret:print(%s 这是一对正确的标签 % ret.group())else:print(%s 这是⼀对不正确的标签 % label) 结果
htmlhh/html 这是一对正确的标签 htmlhh/htmlbalabala 这是⼀对不正确的标签 例子2匹配出 htmlh1www.itcast.cn/h1/html
import re
labels [htmlh1www.itcast.cn/h1/html, htmlh1www.itcast.cn/h2/html]
for label in labels:ret re.match(r(\w*)(\w*).*/\2/\1, label)if ret:print(%s 是符合要求的标签 % ret.group())else:print(%s 不符合要求 % label) 结果
htmlh1www.itcast.cn/h1/html 是符合要求的标签 htmlh1www.itcast.cn/h2/html 不符合要求 举例(?Pname) (?Pname)
一个用于标记一个用于在同一个正则表达式中复用
import re
ret re.match(r(?Pname1\w*)(?Pname2\w*).*/(?Pname2)/(?Pname1),htmlh1www.itcast.cn/h1/html)
ret.group()
ret re.match(r(?Pname1\w*)(?Pname2\w*).*/(?Pname2)/(?Pname1),htmlh1www.itcast.cn/h2/html)
#ret.group()
re.compile 函数
compile 函数用于编译正则表达式生成一个正则表达式 Pattern 对象供 match() 和 search() 这两个函数使用。
prog re.compile(pattern)
result prog.match(string)
等价于
result re.match(pattern, string)
举例
import repattern re.compile(r\d)
m pattern.match(one12twothree34four, 3, 10) # 从1的位置开始匹配正好匹配print m # 返回一个 Match 对象
_sre.SRE_Match object at 0x10a42aac0m.group(0) # 可省略 0
12m.start(0) # 可省略 0
3m.end(0) # 可省略 0
5m.span(0) # 可省略 0
(3, 5)
在上面当匹配成功时返回一个 Match 对象其中
group([group1, …]) 方法用于获得一个或多个分组匹配的字符串当要获得整个匹配的子串时可直接使用 group() 或 group(0)start([group]) 方法用于获取分组匹配的子串在整个字符串中的起始位置子串第一个字符的索引参数默认值为 0end([group]) 方法用于获取分组匹配的子串在整个字符串中的结束位置子串最后一个字符的索引1参数默认值为 0span([group]) 方法返回 (start(group), end(group))
re.search函数
re.search 扫描整个字符串并返回第一个成功的匹配如果没有匹配就返回一个 None。
re.match与re.search的区别re.match只匹配字符串的开始如果字符串开始不符合正则表达式则匹配失败函数返回None而re.search匹配整个字符串直到找到一个匹配
举例
import re
ret re.search(r\d, 阅读次数为9999)
print(ret.group())
结果
9999
re.findall函数
在字符串中找到正则表达式所匹配的所有子串并返回一个列表如果没有找到匹配的则返回空列表。注意 match 和 search 是匹配一次 findall 匹配所有。
举例
import re
ret re.findall(r\d, python 9999, c 7890, c 12345)
print(ret)
结果
[9999, 7890, 12345]
re.finditer函数
和 findall 类似在字符串中找到正则表达式所匹配的所有子串并把它们作为一个迭代器返回。
import re
it re.finditer(r\d, 12a32bc43jf3)
for match in it:print(match.group())
结果
12 32 43 3
re.sub函数
sub是substitute的所写表示替换将匹配到的数据进⾏替换。
语法re.sub(pattern, repl, string, count0, flags0)
参数描述pattern必选表示正则中的模式字符串repl必选就是replacement要替换的字符串也可为一个函数string必选被替换的那个string字符串count可选参数count 是要替换的最大次数必须是非负整数。如果省略这个参数或设为 0所有的匹配都会被替换flag可选参数标志位用于控制正则表达式的匹配方式如是否区分大小写多行匹配等等。
举例将匹配到的阅读次数加1
方法一
import re
ret re.sub(r\d, 998, python 997)
print(ret)
结果python 998
方法二
import re
def add(temp):#int参数必须是字符串类似字节的对象或数字而不是“re.Match”strNum temp.group()num int(strNum) 1return str(num)
ret re.sub(r\d, add, python 997)
print(ret)
ret re.sub(r\d, add, python 99)
print(ret)
结果;
python 998 python 100
re.subn函数
行为与sub()相同但是返回一个元组 (字符串, 替换次数)。
re.subn(pattern, repl, string[, count])
返回(sub(repl, string[, count]), 替换次数)
import re
pattern re.compile(r(\w) (\w))
s i say, hello world!
print(re.subn(pattern, r\2 \1, s))
def func(m):return m.group(1).title() m.group(2).title()
print(re.subn(pattern, func, s))
### output ###
# (say i, world hello!, 2)
# (I Say, Hello World!, 2)
re.split函数
根据匹配进⾏切割字符串并返回⼀个列表。
re.split(pattern, string, maxsplit0, flags0)
参数描述pattern匹配的正则表达式string要匹配的字符串maxsplit分隔次数maxsplit1 分隔一次默认为 0不限制次数
举例
import re
ret re.split(r:| ,info:xiaoZhang 33 shandong)
print(ret)
结果[info, xiaoZhang, 33, shandong]
python贪婪和⾮贪婪
Python⾥数量词默认是贪婪的在少数语⾔⾥也可能是默认⾮贪婪总是尝试匹配尽可能多的字符⾮贪婪则相反总是尝试匹配尽可能少的字符。
例如正则表达式”ab*”如果用于查找”abbbc”将找到”abbb”。而如果使用非贪婪的数量词”ab*?”将找到”a”。
注我们一般使用非贪婪模式来提取。
在*,?,,{m,n}后⾯加上使贪婪变成⾮贪婪。
举例1
import re
sThis is a number 234-235-22-423
#正则表达式模式中使⽤到通配字那它在从左到右的顺序求值时会尽量“抓取”满⾜匹配最⻓字符串在我们上⾯的例⼦⾥⾯“.”会从字符串的启始处抓取满⾜模式的最⻓字符其中包括我们想得到的第⼀个整型字段的中的⼤部分“\d”只需⼀位字符就可以匹配所以它匹配了数字“4”⽽“.”则匹配了从字符串起始到这个第⼀位数字4之前的所有字符
rre.match(.(\d-\d-\d-\d),s)
print(r.group(1))
#⾮贪婪操作符“”这个操作符可以⽤在*,,?的后⾯要求正则匹配的越少越好
rre.match(.?(\d-\d-\d-\d),s)
print(r.group(1))
结果
4-235-22-423 234-235-22-423
举例2 re.match(raa(\d),aa2343ddd).group(1)
2343re.match(raa(\d?),aa2343ddd).group(1)
2re.match(raa(\d)ddd,aa2343ddd).group(1)
2343re.match(raa(\d?)ddd,aa2343ddd).group(1)
2343
举例3提取图片地址
import re
test_strimg data-originalhttps://rpic.douyucdn.cn/appCovers/2016/11/13/1213973.jpg
ret re.search(rhttps://.*?.jpg, test_str)
print(ret.group())结果https://rpic.douyucdn.cn/appCovers/2016/11/13/1213973.jpg
r的作⽤
与大多数编程语言相同正则表达式里使用”\”作为转义字符这就可能造成反斜杠困扰。假如你需要匹配文本中的字符”\”那么使用编程语言表示的正则表达式里将需要4个反斜杠”\\\\”前两个和后两个分别用于在编程语言里转义成反斜杠转换成两个反斜杠后再在正则表达式里转义成一个反斜杠。Python里的原生字符串很好地解决了这个问题Python中字符串前⾯加上 r 表示原⽣字符串。
import re
mm c:\\a\\b\\c
print(mm)#c:\a\b\c
ret re.match(c:\\\\,mm).group()
print(ret)#c:\
ret re.match(c:\\\\a,mm).group()
print(ret)#c:\a
ret re.match(rc:\\a,mm).group()
print(ret)#c:\a
ret re.match(rc:\a,mm).group()
print(ret)#AttributeError: NoneType object has no attribute group
文章转载自: http://www.morning.qckwj.cn.gov.cn.qckwj.cn http://www.morning.bksbx.cn.gov.cn.bksbx.cn http://www.morning.bphqd.cn.gov.cn.bphqd.cn http://www.morning.kpbq.cn.gov.cn.kpbq.cn http://www.morning.ycwym.cn.gov.cn.ycwym.cn http://www.morning.rxfgh.cn.gov.cn.rxfgh.cn http://www.morning.pymff.cn.gov.cn.pymff.cn http://www.morning.wwkdh.cn.gov.cn.wwkdh.cn http://www.morning.prysb.cn.gov.cn.prysb.cn http://www.morning.cykqb.cn.gov.cn.cykqb.cn http://www.morning.ltypx.cn.gov.cn.ltypx.cn http://www.morning.yrskc.cn.gov.cn.yrskc.cn http://www.morning.clkyw.cn.gov.cn.clkyw.cn http://www.morning.hmdn.cn.gov.cn.hmdn.cn http://www.morning.wtcd.cn.gov.cn.wtcd.cn http://www.morning.hlfnh.cn.gov.cn.hlfnh.cn http://www.morning.qggxt.cn.gov.cn.qggxt.cn http://www.morning.wrtw.cn.gov.cn.wrtw.cn http://www.morning.xqknl.cn.gov.cn.xqknl.cn http://www.morning.hmqjj.cn.gov.cn.hmqjj.cn http://www.morning.bktly.cn.gov.cn.bktly.cn http://www.morning.hgscb.cn.gov.cn.hgscb.cn http://www.morning.wgzgr.cn.gov.cn.wgzgr.cn http://www.morning.phxdc.cn.gov.cn.phxdc.cn http://www.morning.rjmb.cn.gov.cn.rjmb.cn http://www.morning.ytfr.cn.gov.cn.ytfr.cn http://www.morning.yxnfd.cn.gov.cn.yxnfd.cn http://www.morning.qnkqk.cn.gov.cn.qnkqk.cn http://www.morning.rfbq.cn.gov.cn.rfbq.cn http://www.morning.lmrjn.cn.gov.cn.lmrjn.cn http://www.morning.gmswp.cn.gov.cn.gmswp.cn http://www.morning.kwblwbl.cn.gov.cn.kwblwbl.cn http://www.morning.ghslr.cn.gov.cn.ghslr.cn http://www.morning.czgfn.cn.gov.cn.czgfn.cn http://www.morning.bfjtp.cn.gov.cn.bfjtp.cn http://www.morning.zbmcz.cn.gov.cn.zbmcz.cn http://www.morning.gfkb.cn.gov.cn.gfkb.cn http://www.morning.rbnp.cn.gov.cn.rbnp.cn http://www.morning.rngyq.cn.gov.cn.rngyq.cn http://www.morning.srhqm.cn.gov.cn.srhqm.cn http://www.morning.qptbn.cn.gov.cn.qptbn.cn http://www.morning.qmnjn.cn.gov.cn.qmnjn.cn http://www.morning.xglgm.cn.gov.cn.xglgm.cn http://www.morning.ntzfj.cn.gov.cn.ntzfj.cn http://www.morning.yrjxr.cn.gov.cn.yrjxr.cn http://www.morning.fxygn.cn.gov.cn.fxygn.cn http://www.morning.hxpff.cn.gov.cn.hxpff.cn http://www.morning.ftmly.cn.gov.cn.ftmly.cn http://www.morning.mdpcz.cn.gov.cn.mdpcz.cn http://www.morning.hjjkz.cn.gov.cn.hjjkz.cn http://www.morning.pwxkn.cn.gov.cn.pwxkn.cn http://www.morning.hprmg.cn.gov.cn.hprmg.cn http://www.morning.mtbth.cn.gov.cn.mtbth.cn http://www.morning.pmlgr.cn.gov.cn.pmlgr.cn http://www.morning.tqpr.cn.gov.cn.tqpr.cn http://www.morning.frxsl.cn.gov.cn.frxsl.cn http://www.morning.deupp.com.gov.cn.deupp.com http://www.morning.kfstq.cn.gov.cn.kfstq.cn http://www.morning.rhpy.cn.gov.cn.rhpy.cn http://www.morning.yrjhr.cn.gov.cn.yrjhr.cn http://www.morning.qbjgw.cn.gov.cn.qbjgw.cn http://www.morning.clfct.cn.gov.cn.clfct.cn http://www.morning.bwxph.cn.gov.cn.bwxph.cn http://www.morning.yxyyp.cn.gov.cn.yxyyp.cn http://www.morning.fjlsfs.com.gov.cn.fjlsfs.com http://www.morning.hcwjls.com.gov.cn.hcwjls.com http://www.morning.nxrgl.cn.gov.cn.nxrgl.cn http://www.morning.qlck.cn.gov.cn.qlck.cn http://www.morning.xnzmc.cn.gov.cn.xnzmc.cn http://www.morning.jghty.cn.gov.cn.jghty.cn http://www.morning.ddgl.com.cn.gov.cn.ddgl.com.cn http://www.morning.rkbly.cn.gov.cn.rkbly.cn http://www.morning.ntdzjx.com.gov.cn.ntdzjx.com http://www.morning.tqldj.cn.gov.cn.tqldj.cn http://www.morning.xqjrg.cn.gov.cn.xqjrg.cn http://www.morning.kgcss.cn.gov.cn.kgcss.cn http://www.morning.rtbx.cn.gov.cn.rtbx.cn http://www.morning.kqylg.cn.gov.cn.kqylg.cn http://www.morning.bpmtq.cn.gov.cn.bpmtq.cn http://www.morning.nwynx.cn.gov.cn.nwynx.cn