re 模块

import re
re.findall()  查询到所有符合的
re.search
re.match
re.compile
flags 修改正则的匹配规则

特殊符号

    "."      除换行符外的任意字符
    "^"      匹配开头
    "$"      匹配结尾
    "*"      重复前一个字符0或尽可能多次
    "+"      重复前一个字符1或尽可能多次
    "?"      重复前一个字符0或1次
    *?,+?,?? 非贪婪模式
    {m,n}    匹配前一个字符m~n次,贪婪模式
    {m,n}?   {m,n}的非贪婪模式,等同于{m}
    "\\"     转义\
    [a-z]    匹配a-z
    [^a-z]   匹配非a-z 
    A|B 	 匹配A或B
    (...)    分组,可以在后面取回
    (?aiLmsux) Set the A, I, L, M, S, U, or X flag for the RE (see below).
    (?:...)  Non-grouping version of regular parentheses.
    (?P<name>...) 设置命名分组
    (?P=name)     Matches the text matched earlier by the group named name.
    (?#...)  A comment; ignored.
    (?=...)  Matches if ... matches next, but doesn't consume the string.
    (?!...)  Matches if ... doesn't match next.
    (?<=...) Matches if preceded by ... (must be fixed length).
    (?<!...) Matches if not preceded by ... (must be fixed length).
    (?(id/name)yes|no) Matches yes pattern if the group with id/name matched,
                       the (optional) no pattern otherwise.

    \number  匹配分组内容.
    \A       只匹配开头,类似^
    \Z       只匹配结尾,类似$
    \b       Matches the empty string, but only at the start or end of a word.
    \B       Matches the empty string, but not at the start or end of a word.
    \d       匹配数字,等价于 [0-9]
    \D       匹配非数字,等价于 [^\d].
    \s       匹配所有空格,等价于 [ \t\n\r\f\v]
    \S       匹配所有非空格,等价于[^\s].
    \w       匹配字符数字下划线,等价于 [a-zA-Z0-9_]
    \W       Matches the complement of \w.
    \\       匹配\



group(0)  打所有分组信息
group(1)  打一个分组信息
group('name') 打印命名分组名称为name的内容

posted @ 2022-01-12 14:42  mingtian是吧  阅读(21)  评论(0编辑  收藏  举报