加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式中的search、findall、finditer 区别

发布时间:2020-12-14 01:26:24 所属栏目:百科 来源:网络整理
导读:方法/属性 作用 match() 决定 RE 是否在字符串刚开始的位置匹配 search() 扫描字符串,找到这个 RE 匹配的位置 findall() 找到 RE 匹配的所有子串,并把它们作为一个列表返回 finditer() 找到 RE 匹配的所有子串,并把它们作为一个迭代器返回 import sys;imp

方法/属性 作用
match() 决定 RE 是否在字符串刚开始的位置匹配
search() 扫描字符串,找到这个 RE 匹配的位置
findall() 找到 RE 匹配的所有子串,并把它们作为一个列表返回
finditer() 找到 RE 匹配的所有子串,并把它们作为一个迭代器返回


import sys;
import re;
if __name__ == '__main__':
    strVal = '''<a href="http://www.andylin02.com" target="_blank" class="dsdfv">aaaa</a>
<a href="http://www.congfeng.com" target="_blank" class="tdsfv">bbbbsdf</a>
<a href="http://www.st.com" target="_blank" class="txx">ccccccc</a>
<a href="http://www.qs.com" target="_blank" class="xxx">ddddd</a>    
    ''';
       
    print(strVal);
    
    strPattern = r"(<s*as.*hrefs*=.*)"; 
    
    #search    
    mtReSearch = re.search(strPattern,strVal);
    
    print("======================= search result =======================");
    print("%s ==> %d" %(mtReSearch.groups(),len(mtReSearch.groups()) ));
    
    #findall
    lsFind = re.findall(strPattern,strVal);
    print("n===================== findall result ========================");
    print("%s ==> %d" %(lsFind,len(lsFind)));
    
    #finditer
    print("n===================== finditer result =======================")
    for mtFind in re.finditer(strPattern,strVal):
        print(mtFind.groups());
        print("------");
结果:
>>> 
<a href="http://www.andylin02.com" target="_blank" class="dsdfv">aaaa</a>
<a href="http://www.congfeng.com" target="_blank" class="tdsfv">bbbbsdf</a>
<a href="http://www.st.com" target="_blank" class="txx">ccccccc</a>
<a href="http://www.qs.com" target="_blank" class="xxx">ddddd</a>    
    
======================= search result =======================
('<a href="http://www.andylin02.com" target="_blank" class="dsdfv">aaaa</a>',) ==> 1

===================== findall result ========================
['<a href="http://www.andylin02.com" target="_blank" class="dsdfv">aaaa</a>','<a href="http://www.congfeng.com" target="_blank" class="tdsfv">bbbbsdf</a>','<a href="http://www.st.com" target="_blank" class="txx">ccccccc</a>','<a href="http://www.qs.com" target="_blank" class="xxx">ddddd</a>    '] ==> 4

===================== finditer result =======================
('<a href="http://www.andylin02.com" target="_blank" class="dsdfv">aaaa</a>',)
------
('<a href="http://www.congfeng.com" target="_blank" class="tdsfv">bbbbsdf</a>',)
------
('<a href="http://www.st.com" target="_blank" class="txx">ccccccc</a>',)
------
('<a href="http://www.qs.com" target="_blank" class="xxx">ddddd</a>    ',)
------

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读