Python re module (Regular Expression), pythonre

Source: Internet
Author: User

Python re module (Regular Expression), pythonre

I. Origin (History)

The "originator" of regular expressions may be traced back to early research by scientists on the working principles of human nervous systems. Warren McCulloch from New Jersey and Walter Pitts, born in Detroit, developed a new mathematical method to describe neural networks, they creatively describe neurons in the nervous system as small and simple automatic control elements, thus making a great work innovation. In 1956, a mathematical scientist named Stephen Kleene, based on Warren McCulloch and Walter Pitts's early work, I published a paper titled neural network event representation. I used mathematical symbols called regular expressions to describe this model and introduced the concept of regular expressions. A regular expression is used as an expression to describe it as an "Algebra of a regular set". Therefore, the term "Regular Expression" is used. Later, people found that the work could be applied to other aspects. Ken Thompson applied this result to some early research on computational search algorithms. Ken Thompson is the principal inventor of Unix, the father of the famous Unix. The father of Unix introduced the symbol system into the editor QED, followed by the editor ed on Unix, and finally introduced grep. Jeffrey Friedl further elaborated on this in his book Mastering Regular Expressions (2nd edition) (translated in Chinese: familiar with Regular Expressions, which has been published to the third edition, if you want to learn more about the theory and history of regular expressions, we recommend that you read this book. Since then, regular expressions have been widely used in UNIX or UNIX-like tools, such as Perl. Perl Regular Expressions are derived from the regex compiled by Henry Spencer and have evolved into pcre (Perl Compatible Regular Expressions ), pcre is a library developed by Philip Hazel and used by many modern tools. The first utility of regular expressions is the qed editor in Unix. Then, regular expressions have been widely used and developed in various computer languages or application fields, and have evolved into a beautiful and sound lark in the computer technology forest. The above is a historical description of the origin and development of regular expressions. Today, regular expressions still play a very important role in text-based editors and search tools.

In the past 60 years, regular expressions have gradually evolved from fuzzy and profound mathematical concepts to major functions in various computer tools and software packages. Not only do many UNIX tools support regular expressions, but in the WINDOWS camp for nearly two decades, the idea and application of regular expressions have been supported and embedded in most Windows developer kits! From the regular expression in Microsoft Visual Basic 6 or Microsoft VBScript. the exploration and development in the. NET Framework, the support for regular expressions in WINDOWS products has developed to an unparalleled level, almost all Microsoft developers and all.. NET can all use regular expressions. If you are a computer language contact, you will be in the mainstream operating systems (* nix [Linux, Unix, etc.], Windows, HP, BeOS, etc) mainstream development languages (delphi, Scala, PHP, C #, Java, C ++, Objective-c, Swift, VB, Javascript, Ruby, and Python) in hundreds of millions of applications, you can see the graceful dance of regular expressions.

Ii. Concepts

A regular expression is a logical formula for string operations. It uses predefined characters and combinations of these specific characters to form a "rule string ", this "rule string" is used to express a filtering logic for strings.

Iii. Engine (omitted)

Iv. Characters

Characters can be divided into common characters and metadata:

Common characters: including uppercase and lowercase characters and numbers.

Metacharacters: (below)

 

Metacharacters Description
\ Mark the next character, or a backward reference, or an octal escape character. For example, "\ n" matches \ n. "\ N" matches the line break. The sequence "\" matches "\", and "\ (" matches "(". It is equivalent to the concept of "Escape Character" in multiple programming languages.
^ Matches the start position of the input string. If the Multiline attribute of the RegExp object is set, ^ matches the position after "\ n" or "\ r.
$ Matches the end position of the input string. If the Multiline attribute of the RegExp object is set, $ also matches the position before "\ n" or "\ r.
* Match the previous subexpression any time. For example, zo * can match "z", "zo", and "zoo ".
+ Match the previous subexpression once or multiple times (greater than or equal to 1 time ). For example, "zo +" can match "zo" and "zoo", but cannot match "z ". + Is equivalent to {1 ,}.
? Match the previous subexpression zero or once. For example, "do (es )?" It can match "do" in "do" or "does ".? It is equivalent to {0, 1 }.
{N} N is a non-negative integer. Match n times. For example, "o {2}" cannot match "o" in "Bob", but can match two o in "food.
{N ,} N is a non-negative integer. Match at least n times. For example, "o {2,}" cannot match "o" in "Bob", but can match all o in "foooood. "O {1,}" is equivalent to "o + ". "O {0,}" is equivalent to "o *".
{N, m} Both m and n are non-negative integers, where n <= m. Match at least n times and at most m times. For example, "o {1, 3}" matches the first three o in "fooooood. "O {0, 1}" is equivalent to "o ?". Note that there must be no space between a comma and two numbers.
? When this character is followed by any other delimiter (*, + ,?, The matching mode after {n}, {n ,}, {n, m}) is not greedy. The non-Greedy mode matches as few searched strings as possible, while the default greedy mode matches as many searched strings as possible. For example, for strings "oooo", "o + ?" A single "o" will be matched, while "o +" will match all "o ".
. Match any single character except "\ r \ n. To match any character including "\ r \ n", use a pattern like "[\ s \ S.
(Pattern) Match pattern and obtain this match. The obtained match can be obtained from the generated Matches set. The SubMatches set is used in VBScript, and $0… is used in JScript... $9 attribute. To match the parentheses, use "\ (" or "\)".
(? : Pattern) If the match is not obtained, it matches pattern but does not obtain the matching result. It is not stored for future use. This is useful when you use the "(|)" character to combine all parts of a pattern. For example, "industr (? : Y | ies) "is a simpler expression than" industry | industrial.
(? = Pattern) If the match is not obtained, it is pre-checked in the forward direction and matches the search string at the beginning of any string that matches the pattern. This match does not need to be obtained for future use. For example (? = 95 | 98 | NT | 2000) "can match" Windows "in" Windows2000 ", but cannot match" Windows "in" Windows3.1 ". Pre-query does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, instead of starting after the pre-query characters.
(?! Pattern) Non-get match, forward negative pre-query, match the search string at the beginning of any string that does not match pattern, this match does not need to be obtained for future use. For example, "Windows (?! 95 | 98 | NT | 2000) "can match" Windows "in" Windows3.1 ", but cannot match" Windows "in" Windows2000 ".
(? <= Pattern) Non-get matching, reverse certainly pre-query, similar to positive certainly pre-query, but in the opposite direction. For example, <= 95 | 98 | NT | 2000) Windows can match Windows in 2000Windows, but cannot match Windows in 3.1Windows ".
(? <! Pattern) Non-get match, reverse negative pre-query, similar to forward negative pre-query, only in the opposite direction. For example, "(? <! 95 | 98 | NT | 2000) Windows can match "Windows" in "3.1Windows", but cannot match "Windows" in "2000Windows ". This location is incorrect.
X | y Match x or y. For example, "z | food" can match "z" or "food" (Be cautious here ). "(Z | f) ood" matches "zood" or "food ".
[Xyz] Character Set combination. Match any character in it. For example, "[abc]" can match "a" in "plain ".
[^ Xyz] Negative value character set combination. Match any character not included. For example, "[^ abc]" can match "plin" in "plain ".
[A-z] Character range. Matches any character in the specified range. For example, "[a-z]" can match any lowercase letter in the range of "a" to "z. Note: only when a hyphen is in a character group and appears between two characters can the range of the characters be expressed. If a group starts with a hyphen, it can only represent the character itself.
[^ A-z] Negative character range. Matches any character that is not within the specified range. For example, "[^ a-z]" can match any character that is not in the range of "a" to "z.
\ B Match A Word boundary, that is, the position between a word and a space (that is, the regular expression "match" has two concepts: matching characters and matching positions, here \ B is the matching position ). For example, "er \ B" can match "er" in "never", but cannot match "er" in "verb ".
\ B Match non-word boundary. "Er \ B" can match "er" in "verb", but cannot match "er" in "never ".
\ Cx Match the control characters specified by x. For example, \ cM matches a Control-M or carriage return character. The value of x must be either a A-Z or a-z. Otherwise, c is treated as a literal "c" character.
\ D Match a numeric character. It is equivalent to [0-9]. -P and perl regular expressions must be added to grep.
\ D Match a non-numeric character. It is equivalent to [^ 0-9]. Grep must be added with-Pperl Regular Expression support
\ F Match a form feed. It is equivalent to \ x0c and \ cL.
\ N Match A linefeed. It is equivalent to \ x0a and \ cJ.
\ R Match a carriage return. It is equivalent to \ x0d and \ cM.
\ S Match any invisible characters, including spaces, tabs, and page breaks. It is equivalent to [\ f \ n \ r \ t \ v].
\ S Match any visible characters. It is equivalent to [^ \ f \ n \ r \ t \ v].
\ T Match a tab. It is equivalent to \ x09 and \ cI.
\ V Match a vertical tab. It is equivalent to \ x0b and \ cK.
\ W Match any word characters that contain underscores. Similar to but not equivalent to "[A-Za-z0-9 _]", here the "word" character uses the Unicode Character Set.
\ W Match any non-word characters. It is equivalent to "[^ A-Za-z0-9 _]".
\ Xn Match n, where n is the hexadecimal escape value. The hexadecimal escape value must be determined by the length of two numbers. For example, "\ x41" matches "". "\ X041" is equivalent to "\ x04 & 1 ". The regular expression can be ASCII encoded.
\ Num Matches num, where num is a positive integer. References to the obtained matching. For example, "(.) \ 1" matches two consecutive identical characters.
\ N Identifies an octal escape value or a backward reference. If at least n subexpressions are obtained before \ n, n is backward referenced. Otherwise, if n is an octal digit (0-7), n is an octal escape value.
\ Nm Identifies an octal escape value or a backward reference. If at least one child expression is obtained before \ nm, the nm is backward referenced. If at least n records are obtained before \ nm, n is a backward reference followed by text m. If none of the preceding conditions are met, if n and m are Octal numbers (0-7), \ nm matches the octal escape value nm.
\ Nml If n is an octal number (0-7) and m and l are Octal numbers (0-7), the octal escape value nml is matched.
\ Un Match n, where n is a Unicode character represented by four hexadecimal numbers. For example, \ u00A9 matches the copyright symbol (& copy ;).
\ P {P} Lowercase p indicates the Unicode attribute, which is used as the prefix of the Unicode positive expression. The "P" in the brackets represents one of the seven character attributes of the Unicode Character Set: punctuation. The other six attributes are: L: letters, M: markup symbols (usually not appear separately), Z: separators (such as spaces, line breaks, etc.), and S: symbols (such as mathematical symbols and currency symbols); N: Numbers (such as Arabic numerals and Roman numerals); C: other characters.* Note: This syntax is not supported in some languages, for example, javascript.
<> Start (<) and end (>) of the matching word (word ). For example, the regular expression <the> can match "the" in the string "for the wise", but cannot match "the" "in the string" otherwise ". Note: This metacharacter is not supported by all software.
() Defines the expressions between (and) as "group" and saves the characters matching the expression to a temporary region (a regular expression can save up to 9 characters ), they can be referenced using symbols from \ 1 to \ 9.
| Perform logical "Or" (Or) operations on the two matching conditions. For example, the regular expression (him | her) matches "it belongs to him" and "it belongs to her", but does not match "it belongs to them .". Note: This metacharacter is not supported by all software.
+ Match one or more characters that match exactly before it. For example, the regular expression 9 + matches 9, 99, and 999. Note: This metacharacter is not supported by all software.
? Match 0 or 1 character that is exactly before it. Note: This metacharacter is not supported by all software.
{I} {I, j} Matches a specified number of characters defined in the previous expression. For example, the regular expression A [0-9] {3} can match the character "A" followed by A string of exactly three numeric characters, such as A123 and A348, but does not match A1234. The regular expression [0-9] {} matches any four, five, or six consecutive numbers.

V. re Module

 re.compile(Pattern,Flags = 0)

re.DEBUG

re.I

re.IGNORECASE

re. L
re. LOCALE
re. M
re. MULTILINE
re. S
re. DOTALL
re. U
re. UNICODE
re. X
re. VERBOSE
re. search( Pattern, String, Flags = 0)
re. match( Pattern, String, Flags = 0)
re. split( Pattern, String, Maxsplit = 0, Flags = 0)
re. findall( Pattern, String, Flags = 0)
re. finditer( Pattern, String, Flags = 0)
re. sub( Pattern, Repl, String, Count = 0, Flags = 0)
re. subn( Pattern, Repl, String, Count = 0, Flags = 0)
re. escape( String)
re. purge()
Vi. Example
Prog = re. compile (pattern) result = prog. match (string)
Equivalent to result = re. match (pattern, string)

The above is Python's support for regular expressions. Familiar with regular expressions is a skill that every programmer must possess. In this year, no program will deal with strings. I am also in the initial stage, sharing with you, ^ _ ^

In addition, the special constructor in the figure does not provide examples. It is difficult to use these regular expressions. If you are interested, think about how to match a word that does not start with abc, ^ _ ^

**************************************** ********************************

**************************************** ********************************

Regular Expression modifier-optional flag

A regular expression can contain some optional identifier modifiers to control the matching mode. Modifier is specified as an optional flag. Multiple tags can be specified by bitwise OR (|. For example, re. I | re. M is set to I and M:

Modifier Description
Re. I Make matching case insensitive
Re. L Perform locale-aware matching
Re. M Multi-row matching, affecting ^ and $
Re. S Make. Match All characters including line breaks
Re. U Parses Characters Based on the Unicode Character Set. This flag affects \ w, \ W, \ B, \ B.
Re. X This flag gives you a more flexible format so that you can write regular expressions more easily.

 
VII. Application Example:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.