zoukankan      html  css  js  c++  java
  • re 模块 常用正则表达式符号 最常用的匹配语法

       常用正则表达式符号
    1
    '.'     默认匹配除 之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行 2 '^'     匹配字符开头,若指定flags MULTILINE,这种也可以匹配上(r"^a"," abc eee",flags=re.MULTILINE) 3 '$'     匹配字符结尾,或e.search("foo$","bfoo sdfsf",flags=re.MULTILINE).group()也可以 4 '*'     匹配*号前的字符0次或多次,re.findall("ab*","cabb3abcbbac")  结果为['abb', 'ab', 'a'] 5 '+'     匹配前一个字符1次或多次,re.findall("ab+","ab+cd+abb+bba") 结果['ab', 'abb'] 6 '?'     匹配前一个字符1次或0次 7 '{m}'   匹配前一个字符m次 8 '{n,m}' 匹配前一个字符n到m次,re.findall("ab{1,3}","abb abc abbcbbb") 结果'abb', 'ab', 'abb'] 9 '|'     匹配|左或|右的字符,re.search("abc|ABC","ABCBabcCD").group() 结果'ABC' 10 '(...)' 分组匹配,re.search("(abc){2}a(123|456)c", "abcabca456c").group() 结果 abcabca456c 11   12   13 'A'    只从字符开头匹配,re.search("Aabc","alexabc") 是匹配不到的 14 ''    匹配字符结尾,同$ 15 'd'    匹配数字0-9 16 'D'    匹配非数字 17 'w'    匹配[A-Za-z0-9] 18 'W'    匹配非[A-Za-z0-9] 19 's'     匹配空白字符、 、 、 , re.search("s+","ab c1 3").group() 结果 ' ' 20   21 '(?P<name>...)' 分组匹配 re.search("(?P<province>[0-9]{4})(?P<city>[0-9]{2})(?P<birthday>[0-9]{4})","371481199306143242").groupdict("city") 结果{'province': '3714', 'city': '81', 'birthday': '1993'}

    最常用的匹配语法

    1 re.match 从头开始匹配
    2 re.search 匹配包含
    3 re.findall 把所有匹配到的字符放到以列表中的元素返回
    4 re.splitall 以匹配到的字符当做列表分隔符
    5 re.sub      匹配字符并替换 
  • 相关阅读:
    Babelfish
    【动态规划】货币面值
    contest 1.18
    contest 1.17
    contest 1.16
    contest 1.15
    contest 1.14
    contest 1.13
    contest 12.31
    [cf]Round #529 (Div. 3)
  • 原文地址:https://www.cnblogs.com/shiluoliming/p/6395219.html
Copyright © 2011-2022 走看看