学习一段python正則表達式了, 对match、search、findall、finditer等函数作一小结
以下以一段网页为例,用python正則表達式作一个范例:
strHtml = ''' '''print strHtml#正則表達式 匹配如:< a href=”xxxxx” class=”xxxx”remod = re.compile(r"
search方法举例
search 会查找第一个找到匹配字符串并返回
item = remod.search(strHtml) if item: print item.group()else: print "no match [search]" # 输出:#
match方法举例
match 会从字符串开头匹配查找第一个找到匹配字符串并返回
item = remod.match(strHtml, re.M|re.S) if item: print item.group()else:print "no match [match]"no match [match] # 输出# no match [match]
findall方法举例
Findall查找全部找到匹配字符串并返回一个列表,假设有匹配的组(group),那么它是这个列表下的一个元组
items = remod.findall(strHtml) if items: print items for it in items: print itelse: print "no match [findall]"# 输出# [('/user/student/', 'user-t'), ('/common/mobile/search/', 'sch')]# ('/user/student/', 'user-t')# ('/common/mobile/search/', 'sch')
finditer方法举例
finditer查找全部找到匹配字符串并返回一个group,能够通过下标引用, 以下从1開始
tems = remod.finditer(strHtml if items: for it in items: print "it.group():",it.group() print "it.group(0):",it.group(0) print "it.group(1):",it.group(1) print "it.group(2):",it.group(2)+"\n"else:print "no match [findall]"# 输出# it.group():