python知识捡拾---正则相关

正则

re

  • ret = re.match(r"速度与激情\d",“速度与激情91”)

  • ret.group() ----> ‘速度与激情9’

  • re.match(r"速度与激情[1-8]",“速度与激情9”)

  • re.match(r"速度与激情[1-36-8]",“速度与激情2”).group()

  • re.match(r"速度与激情[\d\w]",“速度与激情ab”).group()

  • re.match(r"速度与激情[\d\w]",“速度与激情5”).group()

  • re.match(r"速度与激情.",“速度与激情!”).group() ‘速度与激情!’

  • re.match(r’速度与激情\d{1,2}’,“速度与激情12”).group() ‘速度与激情12’

  • re.match(r’速度与激情\d{1,2}’,“速度与激情123”).group() ‘速度与激情12’

  • re.match(r"021-\d{8}",“021-12345678”).group() Out[8]: ‘021-12345678’

  • re.match(r"021-?\d{8}",“02112345678”).group() Out[9]: ‘02112345678’

  • html_content=""“fdsfs
    fjskldfjalsd
    jflasdjfasdfk
    kflashdlg;ajsl
    fdfs”""

  • re.match(r".*",html_content).group() Out[11]: 'fdsfs’这里.*匹配除换行之外的任意字符

  • re.match(r".*",html_content,re.S).group() re.S可以匹配所有 输出:‘fdsfs\nfjskldfjalsd\njflasdjfasdfk\nkflashdlg;ajsl\nfdfs’

  • re.match(r"[a-zA-Z0-9_]{4,20}@(163|126).com$",“[email protected]”).group() 输出: ‘[email protected]

  • re.match(r"[a-zA-Z0-9_]{4,20}@(163|126).com$",“[email protected]”).group(1) 输出: ‘126’

  • re.match(r"[a-zA-Z0-9_]{4,20}@(163|126).com$",“[email protected]”).group(0) 输出: ‘[email protected]

  • re.match(r"([a-zA-Z0-9_]{4,20})@(163|126).com$",“[email protected]”).group(1) 输出: ‘laowang’

  • html_str = “

    hahaha

  • re.match(r"<(\w*)>.*",html_str).group() 输出:’

    hahaha

  • html_str = “

    hahaha

  • re.match(r"<(\w*)><(\w*)>.*",html_str).group() 输出:’

    hahaha

  • re.search(r"\d+",“阅读数为 9999,点赞数为:100”).group() 输出: ‘9999’ 找出一个相匹配的

  • re.findall(r"\d+",“python=9999,c=8790,c++=12345”) 输出: [‘9999’, ‘8790’, ‘12345’] 找出所有匹配到的

  • re.sub(r"\d+",“998”,“python=997”) 输出: ‘python=998’

  • re.sub(r"\d+",“998”,“python=997,c++=1024”) 输出:‘python=998,c++=998’

你可能感兴趣的:(python,技术类)