re模塊

```import re '.' 默認匹配除\n之外的任意一個字符,若指定flagDOTALL, 則匹配任意字符,包括換行 '^' 匹配字符開頭,若指定flagsMULTILINE, 這種也可以匹配上(r"^a", "\nabc\neee", flags=re.MULTILINE)(效果和“\A”相同) '$' 匹配字符結尾,或e.search("foo$", "bfoo\nsdfsf", flags=re.MULTILINE).group()也可以 “*” 匹配 * 號前的字符0次或多次,re.findall("ab*", "cabb3abcbbac")結果爲['abb', 'ab', 'a'] '+' 匹配前一個字符1次或多次,re.findall("ab+", "ab+cd+abb+bba")結果['ab', 'abb'] '?' 匹配前一個字符1次或0次 '{m}' 匹配前一個字符m次 '{n,m}' 匹配前一個字符n到m次,re.findall("ab{1,3}", "abb abc abbcbbb")結果'abb', 'ab', 'abb'] '|' 匹配 | 左或 | 右的字符,re.search("abc|ABC", "ABCBabcCD").group()結果'ABC' '(...)' 分組匹配,re.search("(abc){2}a(123|456)c", "abcabca456c").group()結果abcabca456c '\A' 只從字符開頭匹配,re.search("\Aabc", "alexabc")是匹配不到的 '\Z' 匹配字符結尾,同$ '\d' 匹配數字0 - 9 '\D' 匹配非數字 '\w' 匹配[A - Za - z0 - 9] '\W' 匹配非[A - Za - z0 - 9] 's' 匹配空白字符、\t、\n、\r, re.search("\s+", "ab\tc1\n3").group()結果'\t' '(?P<name>...)' 分組匹配re.search("(?P<province>[0-9]{4})(?P<city>[0-9]{2})(?P<birthday>[0-9]{4})", "371481199306143242").groupdict("city") #結果{'province': '3714', 'city': '81', 'birthday': '1993'} #match是從頭開始匹配是不常用型,search是從整個字符串裏搜索是常用型 a=re.match('.',"inet 地址:192.168.12.55 廣播192.168.12.255") #match就是匹配字符串開頭和“^”作用相同 print(a.group())#默認匹配除\n之外的任意一個字符,結果爲i a=re.match('.+',"inet 地址:192.168.12.55 廣播192.168.12.255") print(a.group()) #匹配'.'1次或者多次,結果爲"inet 地址:192.168.12.55 廣播192.168.12.255" a=re.match('\w*',"!inet 地址:192.168.12.55 廣播192.168.12.255") print(a.group())# "*"沒匹配到也有返回,不會報錯。a的值爲''(* 匹配0次或者多次) print(a)# a=re.match('\w+',"!inet 地址:192.168.12.55 廣播192.168.12.255") print(a.group()) #沒匹配到會報錯(+ 匹配1次或者多次) a=re.match('\w?',"!inet 地址:192.168.12.55 廣播192.168.12.255") print(a.group()) #沒匹配到也有返回,不會報錯。a的值爲''(? 匹配0次或者1次) print(a) a=re.match('\w{3}',"inet 地址:192.168.12.55 廣播192.168.12.255") print(a.group())#匹配前一個字符5次,如果匹配不到會報錯 print(a) a=re.match('\w{5,10}',"inet 地址:192.168.12.55 廣播192.168.12.255") print(a.group())#匹配前一個字符5到10次,如果最低的次數匹配不到會報錯。 print(a) a=re.match('INET|inet',"inetINET 地址:192.168.12.55 廣播192.168.12.255") print(a.group()) #從字符串左邊開始匹配。只要匹配到就會返回。 a=re.match('inet2|INET',"inet 地址:192.168.12.55 廣播192.168.12.255") #print(a.group()) #匹配不到會報錯 print(a) a=re.match('inet2|INET',"INETinet2 地址:192.168.12.55 廣播192.168.12.255") print(a.group())# 從字符串左邊開始匹配。只要匹配到就會返回。 a=re.search("(\d{2})(\d{2})(\d{4})","320495688305618974 name:ooxx") print(a.group()) #直接輸出32049568 print(a.groups()) #分組輸出('32', '04', '9568')元組 a=re.search("\A32","320495688305618974 name:ooxx") print(a.group()) #效果和'^'相同 a=re.search("\A\d.*$","320495688305618974 name:ooxx") print(a.group()) #'$'效果和'\Z'相同 a=re.search("\A\d.*\Z","320495688305618974 name:ooxx") print(a.group()) #'$'效果和'\Z'相同 a=re.search("(?P<province>[0-9]{4})(?P<city>[0-9]{2})(?P<birthday>[0-9]{4})", "371481199306143242").groupdict() #"groupdict()"生成字典 print(a) #“groupdict()”生成字典,“P<city>”是起了一個名字 # match(從字符串開頭開始匹配) a=re.match('.',"inet 地址:192.168.12.55 廣播192.168.12.255") print(a.group()) #group結果爲字符串 # 複雜search匹配(整個字符串匹配) a=re.search('(\d{1,3}\.){3}\d{1,3}',"inet 地址:192.168.12.55 廣播192.168.12.255") print(a.group())#“(\d{1,3}\.){3}”是匹配“(\d{1,3}\.)”3次 # findall(把匹配到的全部取出) a=re.findall("[a-zA-Z]+","1q2w1eqwe4qw5eq3we1q5we4q") print(a) # 把所有英文字母以列表形式取出來 a=re.findall("\D+","1q2w1eqwe\n4qw5eq3we1q5we4q") print(a) # 把所有不是數字的以列表形式取出來['q', 'w', 'eqwe\n', 'qw', 'eq', 'we', 'q', 'we', 'q'] # split(以匹配到的字符爲分隔符) a=re.split("\d+","1q2w1eqwe\n4qw5eq3we1q5we4q") print(a) # 把數字作爲分隔符區分出英文字母(會有空格)['', 'q', 'w', 'eqwe\n', 'qw', 'eq', 'we', 'q'] # sub (匹配並替換)結果爲字符串 a=re.sub("\d+","|","1q2w1eqwe4qw5eq3we1q5we4q") print(a) # 把所有的字符串匹配並替換成“|” a=re.sub("\d+","|","1q2w1eqwe4qw5eq3we1q5we4q",count=4) print(a) # 把所有的字符串匹配4次並替換成“|” # 斜槓困擾(4個斜槓匹配一個斜槓) a=re.split("\\\\","c:\d\c\e\b\c\d") print(a) # ['c:', 'd', 'c', 'e\x08', 'c', 'd'] a=re.split("\\\\",r'c:\d\c\e\rb\oo') # 做好在前面加個“r"” print(a) # ['c:', 'd', 'c', 'e', 'rb', 'oo'] # re.I (不區分大小寫) a=re.search("A","AAaBaC",flags=re.I) print(a.group()) # re.M (匹配多行) a=re.sub(r"a","|", "\nbac\nae", flags=re.M) print(a) # re.S(讓“.”匹配任意字符) a=re.search(r".+", "\nbac\nae") print(a) a=re.search(r".+", "\nbac\nae", flags=re.S) print(type(a.group()))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章