import re #import re模块是为了使用正则表达式,正则表达式本身是一种专门匹配字符串的语言 #正则表达式的方法: #findall(): 所有结果都返回到一个列表里 #search():返回匹配到的第一个对象(object),对象可以调用group()返回结果 #match():只在字符串开始匹配 #...... ret = re.findall('aa','aaaaa') print(ret) #正则表达式元字符 . ^ $ * + ? {} [] | () \ #. 代指除换行符外的任意1个字符 ret1 = re.findall('y..','Thank you very much') print(ret1) #^ 指定匹配一定要从字符串头开始,不能取中间 ret2 = re.findall('^y..','Thank you very much') print(ret2) #$ 指定匹配一定要从字符串末开始,不能取中间 ret3 = re.findall('.ch$','Thank you very much') print(ret3) #.* 指任意0或多个字符 ret4 = re.findall('ab.*','aaaabhghabfb') print(ret4) #+ 指任意1或多个字符 ret4 = re.findall('a+b','aaabhghabfb') print(ret4) #? 0或1个?前的字符 ret5 = re.findall('a?b','aaabhghabfb') print(ret5) #{} 字符重复次数,可以是个范围 ret6 = re.findall('a{2}b','aaabhghabfb') print(ret6) ret7 = re.findall('a{1,2}b','aaabhghabfb') print(ret7) #[] []中的字符进行多选一,[]中的特殊字符,除\ ^ -这个三个外,其他会被取消特殊功能,当做普通字符看待 ret8 = re.findall('a[a,b,c,d]','aaabhghabfb') print(ret8) ret9 = re.findall('a[a-d]','aaabhghabfb') print(ret9) #^放在[]里,表示取反 ret10 = re.findall('[^ab]','aaabhghabfb') print(ret10) #\后跟元字符去除特殊功能 #\后跟部分普通符增加特殊功能 ret10 = re.findall('[^ab]','aaabhghabfb') print(ret10) #\b 匹配一个特殊字符边界 ret11 = re.findall(r'\bI','Hello, Iam a LIST.') print(ret11) # 匹配\的方式 ret12 = re.findall(r'\\','ab\cd\e.') print(ret12) ret13 = re.findall('\\\\','ab\cd\e.') print(ret13) ret14 = re.search('(?P<id>\d{3})w(?P<name>\w{3})','weeew34ttt123wooo') print(ret14.group()) print(ret14.group('id')) print(ret14.group('name')) # | 或 print(re.search('(as)|3','as3').group()) #这里as用()括起来看作一个整体,as3既有as也有3,那个先匹配到先打印哪个 print(re.search('(as)|3','3as').group()) ret15 = re.match('asd','asdqwewqewqerg') print(ret15) ret16 = re.match('asd','fasdqwewqewqerg') print(ret16) ret17 = re.split('[dw]','fasdqwewqewqerg') #用d分割完后,用w分 print(ret17) ret18 = re.sub('a..x','sb','hfalexewqewqe') #用于替换 print(ret18) ret19 = re.compile('\.com') print(ret19.findall('fasdsa.comewrew')) ret20 = re.subn('\d','sb','hfale4xe6wqewqe') #用于多次替换,并返回替换次数 print(ret20) ret21 = re.finditer('\d','dsad2fd324a') print(ret21) print(next(ret21).group())
输出:
['aa', 'aa']
['you', 'y m']
[]
['uch']
['abhghabfb']
['aaab', 'ab']
['ab', 'ab', 'b']
['aab']
['aab', 'ab']
['aa', 'ab', 'ab']
['aa', 'ab', 'ab']
['h', 'g', 'h', 'f']
['h', 'g', 'h', 'f']
['I']
['\\', '\\']
['\\', '\\']
123wooo
123
ooo
as
3
<re.Match object; span=(0, 3), match='asd'>
None
['fas', 'q', 'e', 'qe', 'qerg']
hfsbewqewqe
['.com']
('hfalesbxesbwqewqe', 2)
<callable_iterator object at 0x000002887BD469E8>
2