python 正则表达式 re.findall &re.finditer
语法:
findall 搜索string,以列表形式返回全部能匹配的子串
re.findall(pattern, string[, flags])
finditer 搜索string,返回一个顺序访问每一个匹配结果(Match对象)的迭代器
re.finditer(pattern, string[, flags])
Case:
1 #!use/bin/python 2 #coding:utf-8 3 4 import re 5 6 str= "https://i.cnb1logs.co2m/Edi3tPosts.asp4x?opt=999" 7 print str 8 9 #正则匹配 单词字符+数字 10 pattern =re.compile(r"(\w+)(\d+)") 11 12 #findall全能匹配的list 13 print 'findall (\w+)(\d+) :' ,re.findall(pattern,str) 14 print type(re.findall(pattern,str)) 15 for x in re.finditer(pattern,str): 16 print 'finditer (\w+)(\d+) :' ,x.group()
Output
1 https://i.cnb1logs.co2m/Edi3tPosts.asp4x?opt=999 2 findall (\w+)(\d+) : [('cnb', '1'), ('co', '2'), ('Edi', '3'), ('asp', '4'), ('99', '9')] 3 <type 'list'> 4 finditer (\w+)(\d+) : cnb1 5 finditer (\w+)(\d+) : co2 6 finditer (\w+)(\d+) : Edi3 7 finditer (\w+)(\d+) : asp4 8 finditer (\w+)(\d+) : 999 9 10 ***Repl Closed***
quote:http://cuiqingcai.com/977.html