PYTHON 3.6 re(正则表达式)翻译

来源:互联网 发布:阿里备案域名交易 编辑:程序博客网 时间:2024/05/30 23:01

正则表达式是一种对字符串进行操作的逻辑公式,用事先约定好某些规则,编写逻辑公式,可以用来匹配符合这种逻辑的字符串。

1.正则表达式基础

参考网上2.x的版本重新翻译了一下python 3.6 版本re模块关于正则表达式的介绍,如有不对,敬请指正。

  (API文档打开方式:cmd下输入:python -m pydoc -p 4888  ,按B在游览器打开)//4888是打开的端口号

   "."      Matches any character except a newline.

匹配除换行符外的任意单个字符
     "^"      Matches the start of the string.

匹配字符串的起始位置
     "$"      Matches the end of the string or just before the newline atthe end of the string.

匹配字符串的末尾
     "*"      Matches 0 or more (greedy) repetitions of the preceding RE.
             Greedy means that it will match as many repetitions as possible.

重复匹配前一个字符0~n次,贪婪模式下无限匹配合适的字符串
     "+"      Matches 1 or more (greedy) repetitions of the preceding RE.

重复匹配前一个字符1~n次
     "?"      Matches 0 or 1 (greedy) of the preceding RE.

匹配前一个字符0或1次
     *?,+?,?? Non-greedy versions of the previous three special characters.

使*,+,?变成非贪婪模式
     {m,n}    Matches from m to n repetitions of the preceding RE.

重复匹配前一个字符m~n次,m,n可以省略,省略m匹配0~n次,省略n,匹配m到无限次
     {m,n}?   Non-greedy version of the above.

{m,n}的非贪婪模式
     " \\"     Either escapes special characters or signals a special sequence.

转义字符或者符号化一个特殊的序列
     [ ]       Indicates a set of characters.


         A "^" as the first character indicates a complementing set.

字符集类,对应位置可以是任意字符,字符集中的字符可以集中列出也可以给出范围,以’^’为开始的表示取他的补集

     "|"      A|B, creates an RE that will match either A or B.

匹配满足A或者B的一个字符串
     (...)    Matches the RE inside the parentheses.
             The contents can be retrieved or matched later in the string.

将括起来的表达式作为一个分组,从表达式左边开始遇到的每一个分组的左括号’(’,编号+1,一个分组作为一个整体可视为一个字符进行操作
     (?aiLmsux) Set the A, I, L, M, S, U, or X flag for the RE (see below).

aiLmsux代表不同的匹配模式,可选多个,具体会在下文介绍
     (?:...)  Non-grouping version of regular parentheses.
(…)的非分组模式

    (?P<name>...) The substring matched by the group is accessible by name.

给分组命名
     (?P=name)     Matches the text matched earlier by the group named name.

引用名字为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.
  返回yes如果id/name匹配到,否则返回no
The special sequences consist of "\\" and a character from the listbelow.  

由‘//’和下列特殊字符组成的字符串

If the ordinary character is not on the list, then the
resulting RE will match the second character.
     \number  Matches the contents of the group of the same number.

匹配组中包含number的字符串


     \A       Matches only at the start of the string.

匹配字符串的开始
     \Z       Matches only at the end of the string.

匹配字符串的结束
     \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       Matches any decimal digit; equivalent to the set [0-9] in
             bytes patterns or string patterns with the ASCII flag.

匹配任意十进制数,
             In string patterns without the ASCII flag, it will match the whole
             range of Unicode digits.


     \D       Matches any non-digit character; equivalent to [^\d].

匹配非数字字符,等价于[^\d]
     \s       Matches any whitespace character; equivalent to [ \t\n\r\f\v] in
             bytes patterns or string patterns with the ASCII flag.
             In string patterns without the ASCII flag, it will match the whole
             range of Unicode whitespace characters.

匹配空白符,相当于ASCII里的[ \t\n\r\f\v],在Unicode里匹配空白符
     \S       Matches any non-whitespace character; equivalent to [^\s].

匹配任意非空白符
     \w       Matches any alphanumeric character; equivalent to [a-zA-Z0-9_]

匹配所有字母和数字,等价于[a-zA-Z0-9_]
             in bytes patterns or string patterns with the ASCII flag.
             In string patterns without the ASCII flag, it will match the
             range of Unicode alphanumeric characters (letters plus digits
             plus underscore).
             With LOCALE, it will match the set [0-9_] plus characters defined
             as letters for the current locale.
     \W       Matches the complement of \w.

匹配非字母和数字,等价于[^a-zA-Z0-9_]
     \\       Matches a literal backslash.匹配反斜杠


在线正则表达式生成器:http://tool.lu/regex/

0 0
原创粉丝点击