re.findall and re.search的区别

 ######################################################################
 #   Test re.findall() and re.search()
 #   re.findall() will find ALL the matched string
 #       ['123', '123', '234']
 #   re.search() will only return the FIRST matched string
 #       123
 ######################################################################
 def test_findall_search():
     str1 = '123abc123abc234abc'

     re_str = re.compile(r'd+')
     re_findall = re_str.findall(str1)

     print(re_findall)   # ['123', '123', '234']
     re_search = re_str.search(str1)

     print(re_search.group(0))   # 123

output

['123', '123', '234']

 123


原文地址:https://www.cnblogs.com/double12gzh/p/10166233.html