Python正則表達式小结(1)

时间:2023-03-09 23:20:50
Python正則表達式小结(1)

学习一段python正則表達式了, 对match、search、findall、finditer等函数作一小结 



以下以一段网页为例,用python正則表達式作一个范例:

strHtml = '''<div> <a href="/user/student/" class="user-t"><img src="/uploads/avatar/2015/06/082e408c-14fc-11e5-a98d-00163e02100b_big.jpg"></a>
</div> </div>
<div class="navbar-search-btn visible-xs visible-sm">
<a href="/common/mobile/search/" class="sch"></a>
</div>
'''
print strHtml #正則表達式 匹配如:< a href=”xxxxx” class=”xxxx”
remod = re.compile(r"<a href=\"([^\"]*)\" class=\"([^\"]*)\"")

search方法举例

search 会查找第一个找到匹配字符串并返回

item = remod.search(strHtml)

if item:
print item.group()
else:
print "no match [search]" # 输出:
# <a href="/user/student/" class="user-t"

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 it
else:
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(): <a href="/user/student/" class="user-t"
# it.group(0): <a href="/user/student/" class="user-t"
# it.group(1): /user/student/
# it.group(2): user-t # it.group(): <a href="/common/mobile/search/" class="sch"
# it.group(0): <a href="/common/mobile/search/" class="sch"
# it.group(1): /common/mobile/search/
# it.group(2): sch