Python: Regular Expression-3

The original text comes from FishC , and this article is a personal study note.

module-level functions

Using regular expressions doesn't necessarily require creating a pattern object and then calling its match method. The re module also provides some global functions, such as match, search, findall, sub, etc. The first parameter of these functions is a regular expression string, the other parameters are the same as the methods of the same name, and the return value is the same.

If the regular expression is used frequently, it is recommended to use the pattern object mentioned in the previous chapter. If it is only used occasionally, use the global function.

compile flags

(How complicated it feels) Compile flags allow us to fix the way regular expressions work. Under the re module, compilation flags have two names: full name and shorthand. It looks like this (full name before, abbreviation after):

    ASCII, A makes escape characters such as \w,\b,\s,\d only match ASCII characters, not complete Unicode characters;

    DOTALL, S makes . match any character, including newlines; if not used, it will match all characters except newlines;

    IGNORECASE, I match case-insensitive

    LOCALE, L support the current language (regional) setting; make \w,\W,\b,\B depend on the current locale instead of the Unicode database;

    MULTILINE, M Multi-line matching, affects ^, $; usually ^ only matches the beginning of the string, $ matches the end of the string; when this flag is set, ^ matches not only the beginning of the string, but also the beginning of each line. Likewise, $ matches end of string and end of line;

    VERBOSE, X enables verbose regular expressions: regular expressions can be written better and more organized; spaces are ignored (except in character classes and escaped with backslashes); allowed in regular expressions Use annotations.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325644228&siteId=291194637