超详细的python正则表达式,学习这个就够了#附有实例

正则表达式(Regular Expression)是一种文本模式,包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为"元字符")
下面主要讲如何利用元字符进行匹配:

元字符

字符意义
^匹配输入字符串的开始位置

实例:

strs = 'hello python'
result = re.findall('^python', strs)
print(result)
result = re.findall('^hello', strs)
print(result)
结果:
[]
['hello']
字符意义
$匹配输入字符串的结束位置

实例

strs = 'hello python'
result = re.findall('python$', strs)
print(result)
result = re.findall('hello$', strs)
print(result)
结果:
['python']
[]
字符意义
*匹配前面的子表达式零次多次

实例

strs = 'hello python'
result1 = re.findall('hellok*', strs) #匹配前面的子表达式,即字符'k'零次,能匹配上
print("result1: ", result1)
result2 = re.findall('hello*', strs)  #匹配前面的子表达式,即字符'o'一次,能匹配上
print("result2: ", result2)
result3 = re.findall('hellooo*', strs)#匹配前面的子表达式,即字符'o'零次或多次,匹配不上
print("result3: ", result3)
结果:
result1:  ['hello']
result2:  ['hello']
result3:  []
字符意义
+匹配前面的子表达式一次多次

实例

strs = 'hello python'
result1 = re.findall('hellok+', strs) #匹配前面的子表达式,即字符'k'一次或多次,匹配不上
print("result1: ", result1)
result2 = re.findall('hello+', strs)  #匹配前面的子表达式,即字符'o'一次,能匹配上
print("result2: ", result2)
result3 = re.findall('hel+o', strs)   #匹配前面的子表达式,即字符'l'两次,能匹配上
print("result3: ", result3)
结果:
result1:  []
result2:  ['hello']
result3:  ['hello']
字符意义
?匹配前面的子表达式零次一次

实例

strs = 'hello python'
result1 = re.findall('hellok?', strs) #匹配前面的子表达式,即字符'k'零次,能匹配上
print("result1: ", result1)
result2 = re.findall('hello?', strs)  #匹配前面的子表达式,即字符'o'一次,能匹配上
print("result2: ", result2)
result3 = re.findall('hel?o', strs)   #匹配前面的子表达式,即字符'l'零次或一次,不能匹配上
print("result3: ", result3)
结果:
result1:  ['hello']
result2:  ['hello']
result3:  []
字符意义
{n}n 是一个非负整数,匹配确定的 n 次

实例

strs = 'hello python'
result1 = re.findall('hellok{0}', strs) #匹配前面的子表达式,即字符'k'零次,能匹配上
print("result1: ", result1)
result2 = re.findall('hello{1}', strs)  #匹配前面的子表达式,即字符'o'一次,能匹配
print("result2: ", result2)
result3 = re.findall('hello{2}', strs)  #匹配前面的子表达式,即字符'o'两次,能匹配
print("result3: ", result3)
字符意义
{n,}n 是一个非负整数至少匹配n 次

实例

strs = 'hello python'
result1 = re.findall('hellok{0,}', strs) #匹配前面的子表达式,即字符'k'至少零次,能匹配
print("result1: ", result1)
result2 = re.findall('hello{1,}', strs) #匹配前面的子表达式,即字符'o'一次,能匹配
print("result2: ", result2)
result3 = re.findall('hello{2}', strs)  #匹配前面的子表达式,即字符'o'两次,不能匹配上
print("result3: ", result3)
结果:
result1:  ['hello']
result2:  ['hello']
result3:  []
字符意义
{n,m}m 和 n 均为非负整数,其中n <= m最少匹配 n 次最多匹配 m 次

实例

strs = 'hello python'
result1 = re.findall('hellok{1,2}', strs) #匹配前面的子表达式,即字符'k'一次到两次,不能匹配上
print("result1: ", result1)
result2 = re.findall('hello{0,2}', strs) #匹配前面的子表达式,即字符'o'零次到两次,能匹配
print("result2: ", result2)
result3 = re.findall('hello{2,2}', strs) #匹配前面的子表达式,即字符'o'两次,不能匹配上
print("result3: ", result3)
结果:
result1:  []
result2:  ['hello']
result3:  []
字符意义
?当该字符紧跟在任何一个其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 后面时,匹配模式是非贪婪的。非贪婪模式尽可能少的匹配所搜索的字符串,而默认的贪婪模式则尽可能多的匹配所搜索的字符串

实例

strs = 'hellooo python'
result1 = re.findall('hello{1,}? python', strs)
print("result1: ", result1)
result2 = re.findall('hello*?', strs)
print("result2: ", result2)
result3 = re.findall('hello{2,}?', strs)
print("result3: ", result3)
结果:
result1:  ['hellooo python']
result2:  ['hell']
result3:  ['helloo']
字符意义
.匹配除换行符(\n、\r)之外的任何单个字符。要匹配包括 ‘\n’ 在内的任何字符,请使用像"(.|\n)"的模式

实例

strs = 'hello python'
result1 = re.findall('.', strs)
print("result1: ", result1)
result2 = re.findall('.*', strs)
print("result2: ", result2)
结果:
result1:  ['h', 'e', 'l', 'l', 'o', ' ', 'p', 'y', 't', 'h', 'o', 'n']
result2:  ['hello python', '']
字符意义
(pattern)匹配 pattern 并获取这一匹配

实例

strs = 'hello python'
result = re.findall('hell(o) python', strs) #匹配并取括号的匹配内容
结果:
result:  ['o']
字符意义
?:(pattern)匹配 pattern 但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储供以后使用

实例

strs = 'hello python'
result = re.findall('hell(?:o) python', strs) #匹配并取括号的匹配内容
结果:
result:  ['hello python']
字符意义
(?=pattern)正向肯定预查(look ahead positive assert),在任何匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。

实例

strs = "Windows95 Windows98 WindowsNT Windows2000"
result1 = re.findall('Windows(?=95)', strs)
print("result1 : ", result1)
result2 = re.findall('(?=Windows)98', strs)
print("result2 : ", result2)
结果:
result1 :  ['Windows']
result2 :  []
字符意义
(?!pattern)正向否定预查(negative assert),在任何不匹配pattern的字符串开始处匹配查找字符串,这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用

实例

strs = "Windows95"
result1 = re.findall('Windows(?!95)', strs)
print("result1 : ", result1)
result2 = re.findall('Windows(?!31)', strs)
print("result2 : ", result2)
结果:
result1 :  []
result2 :  ['Windows']
字符意义
(?<=pattern)反向(look behind)肯定预查,与正向肯定预查类似,只是方向相反

实例

strs = "Windows95"
result1 = re.findall('(?<=Windows)95', strs)
print("result1 : ", result1)
result2 = re.findall('(?<=Windows)31', strs)
print("result2 : ", result2)
结果:
result1 :  ['95']
result2 :  []
字符意义
(?<!pattern)反向否定预查,与正向否定预查类似,只是方向相反

实例

strs = "95Windows"
result1 = re.findall('(?<!95)Windows', strs)
print("result1 : ", result1)
result2 = re.findall('(?<!31)Windows', strs)
print("result2 : ", result2)
结果:
result1 :  []
result2 :  ['Windows']
字符意义
x|y[xyz][a-z]分别表示为:匹配 x 或 y;字符集合,匹配所包含的任意一个字符;字符范围,匹配指定范围内的任意字符

实例

strs = "hello python"
result1 = re.findall('(h|p)ello', strs)
print("result1 : ", result1)
result2 = re.findall('[e|y]', strs)
print("result2 : ", result2)
result3 = re.findall('[a-z]', strs)
print("result3 : ", result3)
结果:
result1 :  ['h']
result2 :  ['e', 'y']
result3 :  ['h', 'e', 'l', 'l', 'o', 'p', 'y', 't', 'h', 'o', 'n']
字符意义
[^xyz][^a-z]负值字符范围。匹配任何不在指定范围内的任意字符

实例

strs = "hello python123"
result1 = re.findall('[^ey]', strs)
print("result1 : ", result1)
result2 = re.findall('[^a-z]', strs)
print("result2 : ", result2)
结果:
result1 :  ['h', 'l', 'l', 'o', ' ', 'p', 't', 'h', 'o', 'n', '1', '2', '3']
result2 :  [' ', '1', '2', '3']
字符意义
\b匹配一个单词边界,也就是指单词和空格间的位置;例如, ‘er\b’ 可以匹配"never" 中的 ‘er’,但不能匹配 “verb” 中的 ‘er’
\B匹配非单词边界;‘er\B’ 能匹配 “verb” 中的 ‘er’,但不能匹配 “never” 中的 ‘er’
\cx匹配由 x 指明的控制字符,例如, \cM 匹配一个 Control-M 或回车符;x 的值必须为 A-Z 或 a-z 之一;否则,将 c 视为一个原义的 ‘c’ 字符
\d匹配一个数字字符;等价于 [0-9]
\D匹配一个非数字字符;等价于 [^0-9]
\f匹配一个换页符;等价于 \x0c 和 \cL
\n匹配一个换行符;等价于 \x0a 和 \cJ
\r匹配一个回车符;等价于 \x0d 和 \cM
\s匹配任何空白字符,包括空格、制表符、换页符等等;等价于 [ \f\n\r\t\v]
\S匹配任何非空白字符;等价于 [^ \f\n\r\t\v]
\t匹配一个制表符;等价于 \x09 和 \cI
\v匹配一个垂直制表符;等价于 \x0b 和 \cK
\w匹配字母、数字、下划线;等价于’[A-Za-z0-9_]’
\W匹配非字母、数字、下划线;等价于 ‘[^A-Za-z0-9_]’
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值