The function of the module is mainly used for string and text processing, searching, search, substitution, etc.
Review the basic regular expressions.
.: matches any single character in addition to a newline character
*: Match any character, one, 0, multiple can match to get commonly known as greedy mode
+: Match one or more characters before +
|: Match characters before or after |
^: Match the beginning of the line
$: matching End of line
?: Match located in? Previous 0 or one character, does not match multiple characters
\: Is the escape character after \
[]: Any single character in the match [], [0-9] that matches any number 0 to 9
(): The content within () is treated as a whole
{}: The number of times {} is matched, 100[0-9]{3} indicates that a 3-digit number (100-999) is matched arbitrarily after 100
Metacharacters in Python starting with \:
Special sequence Symbols
|
Significance |
\a
|
Match only at start of string |
\z
|
Match only at end of string |
\b
|
Matches an empty string at the beginning or end |
\b
|
Matches an empty string that is not at the beginning or end |
\d
|
equivalent to [0-9] |
\d
|
equivalent to [^0-9] |
\s
|
Match any whitespace characters: [\t\n\r\r\v] |
\s
|
match any non-white-space character: [^\t\n\r\r\v] |
\w
|
Match any number and letter: [A-za-z0-9] |
\w
|
Match any non-numeric and alphabetic: [^a-za-z0-9] |
Regular Expression Syntax table
| Grammar |
Significance |
Description |
| "." |
Any character |
|
| "^" |
String start |
' ^hello ' match ' HelloWorld ' and not match ' aaaahellobbb ' |
| "$" |
End of string |
In the same vein |
"*"
|
0 or more characters (greedy match)
|
<*> Matching <title>chinaunix</title> |
"+"
|
1 or more characters (greedy match )
|
In the same vein
|
"?"
|
0 or more characters (greedy match )
|
In the same vein
|
*?,+?,??
|
the above three takes the first matching result (not greedy match ) |
<*> Matching <title>
|
{M,n}
|
Repeat M to n times for the previous character, {m} can also
|
A{6} matches 6 A, a{2,4} matches 2 to 4 a |
{m,n}?
|
Repeat M to n times for the previous character and take as few
|
' aaaaaa ' a{2,4} will match only 2 |
"\\"
|
Special word escape or special sequence |
|
[]
|
Represents a character set |
[0-9], [A-Z],[a-z], [^0] |
"|"
|
Or |
a| B, or operation |
(...)
|
Match any expression in parentheses |
|
(?#...)
|
Note, you can ignore |
|
(?=...)
|
Matches if ... matches next, but doesn ' t consume the string.
|
' (? =test) ' match Hello in hellotest |
(?! ...)
|
Matches if ... doesn ' t match next.
|
'(?! =test) ' If Hello is not followed by test, match Hello
|
(<= ...)
|
Matches if preceded by ... (must be fixed length).
|
' (? <=hello) test ' match test in Hellotest
|
(?<!...)
|
Matches if not preceded by ... (must be fixed length).
|
' (? <!hello) test ' does not match test in Hellotest
|
Match the logo and meaning
| Sign |
Meaning |
| Re. I |
Ignore case |
| Re. L |
Change \w,\w,\b,\b,\s,\s matches based on local settings |
| Re. M |
Multi-line Matching mode |
| Re. S |
Make "." Metacharacters Match newline characters |
| Re. U |
Match Unicode characters |
| Re. X |
Ignore the spaces in the pattern that you want to match, and you can use the "#" note |
Text content (extract password files under Linux)
Man:x:6:12:man:/var/cache/man:/bin/nologin
There are 3 search functions in the RE module, each of which accepts 3 parameters (matching pattern, string to match, logo to match), and if the match returns an object instance, Shanyo will return none.
FindAll (): Used to find strings in a string that match a regular expression and return a list of those strings
Search (): Searches the entire string, returns the object instance
Match (): Match only from first character, no longer match, return object instance
lovelinux@lovelinux:~/py/boke$ Cat text
man:x:6:12:man:/var/cache/man:/bin/sh
lovelinux@lovelinux:~/py/ boke$ cat test.py
#/usr/bin/env python
#coding: utf-8
import re
with open (' text ', ' r ') as txt:
f = Txt.read ()
print re.match (' bin ', f)
print re.search (' bin ', f). End ()
lovelinux@lovelinux:~/py/boke$ Python test.py
None
lovelinux@lovelinux:~/py/boke$ vim test.py
lovelinux@lovelinux:~/py/ boke$ python test.py
None
<_sre. Sre_match Object at 0x7f12fc9f9ed0>
Return is an object instance with 2 methods.
Start (): Returns the starting index where the record matches to the character
End (): Returns the ending index of a record that matches to a character
lovelinux@lovelinux:~/py/boke$ python test.py
None
lovelinux@lovelinux:~/py/boke$ Cat test.py
#/usr/bin/env python
#coding: utf-8
import re
with open (' text ', ' r ') as txt:
f = txt.read ()
print Re.match (' bin ', f)
print re.search (' bin ', f). Start ()
print re.search (' bin ', f). End ()