正则表达式
作者:互联网
import re
s = '娜扎热巴马儿扎哈佟丽娅'
result = re.match('佟丽娅', s) # 只要从头进行匹配,如果匹配不成功则返回none
print(result)
result = re.search('佟丽娅', s) # search 进行正则字符串匹配方法,匹配的是整个字符串,只返回第一个找到的结果就结束
print(result)
print(result.span()) # 返回位置
print(result.group()) # 提取匹配到的内容
#######################
msg = 'abcd7yjkfd8hdf00'
result = re.search('[a-z][0-9][a-z]', msg) # search 只要有匹配的,后面就不会在进行检索
print(result)
result = re.findall('[a-z][0-9][a-z]', msg) # 匹配所有
print(result)
msg = 'abcd7777777yjk3fd448hdf00'
result = re.findall('[a-z][0-9]+[a-z]', msg) # +:≥1 , *:≥0 ?:0或1
print(result)
# qq
qq = '337845818'
result = re.match('^[1-9][0-9]{4,}$',qq) # ^:锚定词首,$:锚定词尾,{m,n} ≥m,≤n, {m}固定m次
print(result)
------------------------------------
None
<re.Match object; span=(8, 11), match='佟丽娅'>
(8, 11)
佟丽娅
<re.Match object; span=(3, 6), match='d7y'>
['d7y', 'd8h']
['d7777777y', 'k3f', 'd448h']
<re.Match object; span=(0, 9), match='337845818'>
标签:匹配,佟丽娅,re,正则表达式,msg,result,print 来源: https://blog.csdn.net/m0_59750663/article/details/120556093