在线正则表达试测试链接
正则匹配
import re
content= "wen15526474965min"
result= re.match('^w.*n$', content)
print (result)
print (result.group())
print (result.span())
结果为
<_sre.SRE_Match object; span=(0, 17), match='wen15526474965min'>
wen15526474965min
(0, 17)
匹配目标分组匹配
import re
content= "wen 15526474965 min"
result= re.match('^w.*\s(\d+)\smin$', content)
print (result)
print (result.group(1))
print (result.span())
print(len(content))
结果为
<_sre.SRE_Match object; span=(0, 19), match='wen 15526474965 min'>
15526474965
(0, 19)
19
贪婪模式匹配
.*匹配尽可能多的字符
import re
content= "wen 15526474965 min"
result= re.match('^w.*(\d+)\smin$', content)
print (result)
print (result.group(1))
print (result.span())
print(len(content))
结果为
<_sre.SRE_Match object; span=(0, 19), match='wen 15526474965 min'>
5
(0, 19)
19
非贪婪模式匹配
.*?匹配尽可能少的字符
import re
content= "wen 15526474965 min"
result= re.match('^w.*?(\d+)\smin$', content)
print (result)
print (result.group(1))
print (result.span())
print(len(content))
结果为
<_sre.SRE_Match object; span=(0, 19), match='wen 15526474965 min'>
15526474965
(0, 19)
19
匹配模式
re.match加参数re.s可匹配换行符
import re
content= "wen 15526474965 m" \
"in"
result= re.match('^w.*?(\d+).*n$',content,re.S)
print (result)
print(result.group(1))
结果为
<_sre.SRE_Match object; span=(0, 19), match='wen 15526474965 min'>
15526474965
总结
尽量使用泛匹配模式.* 和非贪婪模式 .*? 如果有换行符,match函数加参数re.S。如果要匹配通配符,在通配符前加转义符\
re.compile 将正则表达式转为编译对象
import re
content= "wen 15526474965 m" \
"in"
pattern= re.compile('^w.*?(\d+).*n$',re.S)
result= re.match(pattern,content)
print (result)
print(result.group(1))