1. 程式人生 > 其它 >python3的re正則的簡單使用

python3的re正則的簡單使用

import re
#  match----------------------------------------------------
print(re.match(r'www', 'www.runoob.com').span())  # (0,3)
print(re.match('com', 'www.runoob.com'))  # None

line = "Cats are smarter than dogs"
match_obj = re.match(r'(.*) are (.*?) .*', line, re.M | re.I)  # re.I 表示忽略大小寫,re.M多行匹配,影響 ^ 和 $
if match_obj: print(match_obj.groups()) # ('Cats', 'smarter') print(match_obj.group()) # Cats are smarter than dogs print(match_obj.group(1)) # Cats print(match_obj.group(2)) # smarter else: print("No match!!") # search----------------------------------------------------- print
(re.search('www', 'www.runoob.com').span()) # (0,3) print(re.search('com', 'www.runoob.com').span()) # (11, 14) line = "Cats are smarter than dogs" search_obj = re.search(r'(.*) are (.*?) .*', line, re.M | re.I) if search_obj: print(search_obj.groups()) # ('Cats', 'smarter') print(search_obj.group()) #
Cats are smarter than dogs print(search_obj.group(1)) # Cats print(search_obj.group(2)) # smarter else: print("Nothing found!!") # re.match與re.search的區別 # re.match 只匹配字串的開始,如果字串開始不符合正則表示式,則匹配失敗,函式返回 None,而 re.search 匹配整個字串,直到找到一個匹配。 # sub 替換------------------------------------------------------- phone = "2004-959-559 # 這是一個電話號碼" num = re.sub(r'#.*$', "", phone) print(num) # 2004-959-559 num = re.sub(r'\D', "", phone) print(num) # 2004959559 def double(matched): value = int(matched.group('value')) return str(value * 2) s = 'A23G4HFD567' print(re.sub('(?P<value>\d+)', double, s)) # A46G8HFD1134 # compile -------------------------------------------------------------- pattern = re.compile(r'([a-z]+) ([a-z]+)', re.I) m = pattern.match('Hello World Wide Web') print(m.groups()) # ('Hello', 'World') # findAll ---------------------------------------------------------------- result1 = re.findall(r'\d+', 'runoob 123 google 456') pattern = re.compile(r'\d+') result2 = pattern.findall('runoob 123 google 456') result3 = pattern.findall('run88oob123google456', 0, 10) print(result1) # ['123', '456'] print(result2) # ['123', '456'] print(result3) # ['88', '12'] result = re.findall(r'(\w+)=(\d+)', 'set width=20 and height=10') print(result) # [('width', '20'), ('height', '10')] # finditer ---------------------------------------------------------------- it = re.finditer(r"\d+", "12a32bc43jf3") for match in it: print(match.group()) # 12 32 43 3 # split ------------------------------------------------------------------------ print(re.split('\W+', 'runoob, runoob, runoob.')) # ['runoob', 'runoob', 'runoob', ''] print(re.split('\W+', 'runoob, runoob, runoob.', 1)) # ['runoob', 'runoob, runoob.']