• 王安
    2020-02-01
    re里面的转移字符r是不是只能把正则表达式那一层需要的转义取消掉。
    
    
  • 卡本
    2018-11-28
    请问老师,findall感觉更像是search的拓展,那findall可以用于match和sub么?感觉findall这块不太明白,谢谢老师~

    作者回复:

    findall、match、sub、match分别是不同的函数功能,并非是扩展,例如search函数是找到第一个匹配的字符串,findall是找到所有匹配的字符串
    根据需要使用不同的函数。如下:

    import re
    string = "x abc y 123 z 123"
    pattern = re.compile(r'\d+')
    print(re.search(pattern,string))
    print(pattern.findall(string))

    # 输出结果
    # <re.Match object; span=(8, 11), match='123'>
    # ['123', '123']

    这些函数之间都可以配合使用的。

    
    
我们在线,来聊聊吧