码迷,mamicode.com
首页 > 编程语言 > 详细

Python正则表达式小结(1)

时间:2015-07-30 13:38:04      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:下面表达式   findall   finditer   search   python   

学习一段python正则表达式了, 对matchsearchfindallfinditer等函数作一小结 


下面以一段网页为例,用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






版权声明:本文为博主原创文章,未经博主允许不得转载。

Python正则表达式小结(1)

标签:下面表达式   findall   finditer   search   python   

原文地址:http://blog.csdn.net/xxm524/article/details/47147377

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!