python学习,day5:正则
# coding=utf-8 # Author: RyAn Bi import re res = re.match('Ryan','Ryan123abcdefg456') #匹配2个字符串中相似的部分 print(res) print(res.group()) #只显示相同部分 res1 = re.match('Ryan\d','Ryan123abcdefg456') #显示匹配后面的数字 print(res1) res2 = re.match('Ryan\d+','Ryan123abcdefg456') #显示匹配后面的所有数字 print(res2) res3 = re.match('^.','Ryan123abcdefg456') #^是开头的意思(match都是从头),[^]表示非的意思,.是除了\n外的任意一个字符(这个就是取开头的字符) print(res3) res4 = re.search('b.+e','Ryan123abcdefg456e') #search 去开头b,结尾e的中间的字符 print(res4) res5 = re.search('a[a-z]+g','Ryan123abcdefgAbcdefg456e') #取a开头,g结尾,中间为a-z的字符串 print(res5) res6 = re.search('a[a-zA-Z]+g','Ryan123abcdefgAbcdefg456e') #取a开头,g结尾,中间为a-z和A-Z的字符串 print(res6) res7 = re.search('#.+#','123#hello#') # 两个#之间 print(res7) res8 = re.search('a?','aabce') # 带?,匹配?前的字符0次或1次 print(res8) res9 = re.search('aal?','aabceaaa') # 带?,匹配?前的字符0次或1次(l为0次,就是匹配aa) print(res9) res10 = re.search('[0-9]{3}','aab1c2e3456a5aa') # 匹配0-9,连续3个 print(res10) res11 = re.findall('[0-9]{1,3}','aab1c2e345a5aa') # 匹配0-9,连续1~3个,全部都匹配到 print(res11) res11 = re.search('abc|ABC','abcddABC') # |或的意思 print(res11) res12 = re.findall('abc|ABC','abcddABC') # |或的意思findall print(res12) res13 = re.search('abcd\|','abcd|dABC') # 匹配管道符| print(res13) res14 = re.search('\A[0-9]+[a-z]\Z','123a') # \A开头,\Z结尾,等同$ print(res14) res15 = re.search('\D+','123a、') # \D 非数字 \W匹配非字母数字,\w匹配字母数字,\匹配空白字符 print(res15) res16 = re.search('(?P<id>[0-9]+)','abde1234afd@13').groupdict() # \D 非数字 \W匹配非字母数字,\w匹配字母数字,\匹配空白字符 print(res16) res17 = re.search('(?P<id>[0-9]+)(?P<name>[a-z]+)','abde1234afd@13').groupdict() # 直接匹配成字典 print(res17) print(res17['id']) res18 = re.search('(?P<provice>[0-9]{4})(?P<city>[0-9]{2})(?P<birthday>[0-9]{4})','130603197911010318').groupdict() # 取身份证号码 print(res18) res19 = re.split('[0-9]+','abd124dedg8dsf') #以数字做间隔,形成列表 print(res19) res20 = re.sub('[0-9]+','|','abd124dedg8dsf') #把数字替换为| print(res20) res21 = re.search('[a-z]+','abdEDFGE8dsf',flags=re.I) #flags=re.I,忽略大小写 print(res21) res22 = re.search('[a-z]+f$','abd\nEDFG\nE8dsf',flags=re.M) #flags=re.M,即便有换行,也能匹配 print(res22) res23 = re.search('.+f$','abd\nEDFG\nE8dsf',flags=re.S) #理论上flags=re.S是匹配除了换行符\n之外的,但是windows不行 print(res23) res24 = re.search(r'\d+\.?\d*(\*-?\d+\.?\d*)+', 2*2) print(res24)