Python中re模块的常见方法

re模块的常见方法

1.pattern.match(从头找一个)

    ret = re.match("[1-9]?\d$","08")



2.pattern.search(找一个)

    ret = re.search(r"\d+", "阅读次数为 9999")



3.pattern.findall(找所有) 返回一个列表,没有就是空列表

re.findall("\d","lei0hou1") >> ["0","1"]



4.pattern.sub(替换)

re.sub("\d","_","lei0hou1") >> ["lei_hou_"]



5.re.compile(编译)

  • 返回一个模型P,具有和re一样的方法,但是传递的参数不同
  • 匹配模式需要传到compile中

    p = re.compile("\d",re.S)
    p.findall("chuan1zhi2")

猜你喜欢

转载自blog.csdn.net/xlengji/article/details/80964143