欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python 正则表达式语法学习笔记

程序员文章站 2022-04-11 18:48:07
正则表达式(regular expression)描述了一种字符串匹配的模式(pattern),可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。python...

正则表达式(regular expression)描述了一种字符串匹配的模式(pattern),可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。

python 自1.5版本起增加了re 模块,它提供 perl 风格的正则表达式模式。

re 模块使 python 语言拥有全部的正则表达式功能。

compile 函数根据一个模式字符串和可选的标志参数生成一个正则表达式对象。该对象拥有一系列方法用于正则表达式匹配和替换。

本文重点给大家介绍python 正则表达式语法。

the special characters are:
    "."      matches any character except a newline.
    "^"      matches the start of the string.
    "$"      matches the end of the string or just before the newline at
             the 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.
    "+"      matches 1 or more (greedy) repetitions of the preceding re.
    "?"      matches 0 or 1 (greedy) of the preceding re.
    *?,+?,?? non-greedy versions of the previous three special characters.
    {m,n}    matches from m to n repetitions of the preceding re.
    {m,n}?   non-greedy version of the above.
    "\\"     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.
    (...)    matches the re inside the parentheses.
             the contents can be retrieved or matched later in the string.
    (?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>...) the substring matched by the group is accessible by 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.

the special sequences consist of "\\" and a character from the list
below.  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.
    \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].
    \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.
    \s       matches any non-whitespace character; equivalent to [^\s].
    \w       matches any alphanumeric character; equivalent to [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.
    \\       matches a literal backslash.

this module exports the following functions:
    match     match a regular expression pattern to the beginning of a string.
    fullmatch match a regular expression pattern to all of a string.
    search    search a string for the presence of a pattern.
    sub       substitute occurrences of a pattern found in a string.
    subn      same as sub, but also return the number of substitutions made.
    split     split a string by the occurrences of a pattern.
    findall   find all occurrences of a pattern in a string.
    finditer  return an iterator yielding a match object for each match.
    compile   compile a pattern into a regexobject.
    purge     clear the regular expression cache.
    escape    backslash all non-alphanumerics in a string.

some of the functions in this module takes flags as optional parameters:
    a  ascii       for string patterns, make \w, \w, \b, \b, \d, \d
                   match the corresponding ascii character categories
                   (rather than the whole unicode categories, which is the
                   default).
                   for bytes patterns, this flag is the only available
                   behaviour and needn't be specified.
    i  ignorecase  perform case-insensitive matching.
    l  locale      make \w, \w, \b, \b, dependent on the current locale.
    m  multiline   "^" matches the beginning of lines (after a newline)
                   as well as the string.
                   "$" matches the end of lines (before a newline) as well
                   as the end of the string.
    s  dotall      "." matches any character at all, including the newline.
    x  verbose     ignore whitespace and comments for nicer looking re's.
    u  unicode     for compatibility only. ignored for string patterns (it
                   is the default), and forbidden for bytes patterns.

python 正则表达式语法学习笔记

python 正则表达式语法学习笔记 

下面看下正则表达式匹配的流程:

python 正则表达式语法学习笔记

正则表达式的大致匹配过程是:依次拿出表达式和文本中的字符比较,如果每一个字符都能匹配,则匹配成功;一旦有匹配不成功的字符则匹配失败。如果表达式中有量词或边界,这个过程会稍微有一些不同,但也是很好理解的,自己多使用几次就能明白。

总结

到此这篇关于python 正则表达式语法记录的文章就介绍到这了,更多相关python 正则表达式语法记录内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!