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


posted @ 2015-07-12 19:33  大海星  阅读(364)  评论(0编辑  收藏  举报