1 import re 2 pattern = 'hello' 3 s = 'hello python' 4 m = re.search(pattern,s) 5 print(m) 6 print(m.group()) 7 8 print('--------match和search的区别--------') 9 pattern = 'love' 10 s = 'I love you' 11 m = re.match(pattern,s) 12 print('使用match进行匹配',m) 13 w = re.search(pattern,s) 14 print('使用search进行匹配',w) 15 print(w.group())
1 <re.Match object; span=(0, 5), match='hello'> 2 hello 3 --------match和search的区别-------- 4 使用match进行匹配 None 5 使用search进行匹配 <re.Match object; span=(2, 6), match='love'> 6 love