美文网首页
python->正则表达式

python->正则表达式

作者: yiangdea | 来源:发表于2017-09-14 11:03 被阅读3次

    searchString = "010-123456780"
    regular = r"^010-\d{8}"
    print(re.findall(regular,searchString))

    *代表,将前面的字符,重复零到多次
    +代表,一次到多次
    ?代表一次或者零次
    regular = r"^010-\d*"
    regular = r"^010-[0-9]+"
    regular = r"^010-[0-9]?"
    regular = r"^010-?\d{0,8}"

    匹配电话号码

    regular_tel = r"\d{3,4}-?\d{8}"
    regular_tel = re.compile(regular_tel)
    print(re.findall(regular_tel,searchString))

    searchString = "csvt hellp csvt hello csvt csvt "
    csvt_re = re.compile(r"csvt",re.I)
    print(csvt_re.findall(searchString))
    x = csvt_re.match(searchString)
    print(x.group())

    s = "hello csvt"
    regular = r"c..t"

    s = re.sub(regular,"python",s)
    print(s)

    email_regular = r"\w{3}@\w+(.com|.cn)"
    print(re.match(email_regular,"zzz@123.com"))

    相关文章

      网友评论

          本文标题:python->正则表达式

          本文链接:https://www.haomeiwen.com/subject/rroacxtx.html