zoukankan      html  css  js  c++  java
  • Python正則表達式小结(1)

    学习一段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)+"
    "
    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






  • 相关阅读:
    NSUserDefaults存储自定义类
    beginBackgroundTaskWithExpirationHandle
    instancetype
    #define const extern
    singleton
    报错:说改变了系统文件。解决方法
    不合法语句 self.contentView.frame.origin.x = x;
    google应用商店的解决
    笔记
    读流testDemo
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/7154433.html
Copyright © 2011-2022 走看看