Regular expressions
The essence of the regular expression
1, the essence of the regular expression (or RE) is a small, highly specialized programming language, (in Python) it is embedded in Python, and through the RE module implementation. The regular expression pattern is compiled into a sequence of bytecode, which is then executed by a matching engine written in C.
2. Introduction to Regular expressions
Regular expressions are not part of Python. Regular expressions are powerful tools for working with strings, with their own unique syntax and an independent processing engine, which may not be as efficient as Str's own approach, but very powerful. Thanks to this, in the language that provides the regular expression, the syntax of the regular expression is the same, except that the number of grammars supported by different programming languages is different; but don't worry, the unsupported syntax is usually the less common part.
Two, the regular expression meta-character. (. * +?) ^ $ {} [] \ | ())
1, ". "represents any character in a matching string other than a newline.
Eg:re.findall (' A. ', ' abc ') #返回的结果是ab
2, "+" means to match the previous subexpression one or more times, equivalent to a positive infinity. {1,}
Eg:re.findall (' A + ', ' abc ') #返回的结果是a
3, "*" means matching the previous subexpression 0 or more times, equivalent to 0 to positive infinity. {0,}
Eg:re.findall (' aa* ', ' abc ') #返回的结果是a
4, "? "Indicates that the previous subexpression was matched 0 or one time, equivalent to 0 and one. {0,1}
Eg:re.findall (' AA ', ' abc ') #返回结果为a
5, "^" (caret) indicates where the matching string begins, meaning that only the target string appears at the starting position.
Eg:re.findall (' ^ab ', ' abcabcabc ') #返回的结果是ab
6, "$" (dollar symbol) indicates the end position of the matched string, that is, the position of the end of the target string will match as long as it appears.
Eg:re.findall (' bc$ ', ' abcabc ') #返回的结果为bc
7, "{x, y}" (curly braces) indicates that the previous subexpression is matched to at least X times, with a maximum of y times, where x, Y is non-negative, x<y.
Eg:re.findall (' a{2,4} ', ' Abcaaaabc ') #返回的结果为aaaa
8, "[X, Y]" (square brackets) to match any one of the characters in parentheses, if the match is successful, return the result, where all the special characters inside the parentheses are out of effect, only "-", "\", "^" has its special meaning.
Eg:re.findall (' a[b,c,d]c ', ' ABCADCAFC ') #返回的结果为abc, ADC
Note:-The range is represented in brackets, ^ in brackets, and \ in brackets to preserve its original functionality.
9, "()" indicates that a matching character is to be grouped, matches the regular expression in the parentheses, or specifies the starting and ending positions of a subset (this function is used primarily and \[0-9]).
Eg:re.findall (' R (GE) G ', ' fagrgeg5654yg5h54t3 ') #返回结果为ge
Eg:re.findall (' R (: GE) G ', ' fagrgeg5654yg5h54t3 ') #返回结果为rgeg
Eg:re.search (' R (GE) \1 ', ' fagrgege5654yg5h54t3 ') #返回结果为gege
eg: Ret=re.search (r "-blog-aticles-(? p<year>20[01]\d)-(? p<month>\d+) ","-blog-aticles-2015-04 ") #P是大写的
Print (Ret.group ("Year"))
10, "x|y" means match regular expression x or regular expression Y. The general case is with parentheses.
Eg:re.findall (' AC|BC ', ' ABCDEFG ') #返回的结果为bc
Eg:re.findall (' A (?: b|c) d ', ' ACDBCDEFG ') #返回的结果为acd
11, "\" backslash in the expression of the most widely used, it can either convert a normal character to a special character, you can also convert a special character to a normal character (equivalent to the special function of releasing metacharacters), if the backslash followed by the number of two uses, If the number is 1-99 for the string that refers to the substring of the ordinal, if the number is 0 or three digits (092,123), then he treats the three-digit number as an octal (mainly by converting the octal to the ASCII table's corresponding character, The ASCII encoding table has a total of 127 characters).
Eg:re.findall (' \d+ ', ' afew3223fef45t ') #返回结果为3223, 45
Eg:re.findall (R ' \\d ', ' FAWEF\DFWA ') #返回的结果为 \\d
Eg:re.search (' (GE) \1 ', ' fagrgege5654yg5h54t3 ') #返回结果为ge equivalent to Re.search (' Gege ', ' fagrgege5654yg5h54t3 ') #返回结果为gege
Note: \d matches any decimal number; it is equivalent to class [0-9].
\d matches any non-numeric character; it is equivalent to class [^0-9].
\s matches any whitespace character; it is equivalent to class [\t\n\r\f\v].
\s matches any non-whitespace character; it is equivalent to class [^ \t\n\r\f\v].
\w matches any alphanumeric underscore character; it is equivalent to class [a-za-z0-9_].
\w matches any non-alphanumeric underscore character; it is equivalent to class [^a-za-z0-9_]
\b Matches a special character boundary, such as a space, &,#, etc.
Third, regular expression module
1. Re module
Import re
2. How to use the module
1.re.findall (pattern,string): The module is the result of returning all matching criteria in a list.
2.re.search (pattern,string): The function finds a pattern match in the string until it finds the first matching string, returns the matching object, returns the object's memory address, and wants to see its matching The object must be appended with a. Group () after the returned result object to invoke its string, whose string type is str and returns none if the string is not matched.
3.re.match (pattern,string): function will find at the beginning of the string, if the match successfully returns the memory address of the string, you need to call the memory address with. Group (), return a string of type STR, and return if no match is successful Nonetype
Python Basic---Function Module 3 (regular expression)