This article mainly introduces the Pythonre module. For more information, see
Python escape characters
The regular expression uses the backslash (\) to represent a special form or escape character. this is in conflict with the Python syntax. therefore, python uses "\\\\" to represent "\" in the regular expression, because if the regular expression matches "\", it needs to be escaped with "\", in the Python syntax, each \ in the string needs to be escaped, so it becomes "\".
Is it difficult to write the above code? in order to make the regular expression more readable, Python specially designed the original string (raw string). Please note that, do not use raw string when writing the file path. there is a trap here. Raw string uses 'R' as the string prefix, for example, r "\ n": represents two characters "\" and "n", instead of line breaks. This form is recommended when writing regular expressions in Python.
Metacharacters of regular expressions
. Match any character except the line break ^ match the start of the string $ match the end of the String [] is used to match a specified character category? For the first character to repeat 0 to 1 times * for the first character to repeat 0 to infinite times {} for the previous character to repeat m Times {m, n} The first character is repeated as m to n times. \ d matches numbers, which is equivalent to [0-9] \ D matches any non-numeric characters, equivalent to [^ 0-9] \ s matching any blank space, equivalent to [fv] \ S matching any non-blank characters, equivalent to [^ fv] \ w matching any letter and digit characters, equivalent to [a-zA-Z0-9 _] \ W matching any non-alphanumeric characters, equivalent to [^ a-zA-Z0-9 _] \ B matching word start or end
The module function is used as an example.
Re. compile compiles the regular expression into a pattern object.
compile(pattern, flags=0)
First parameter: Rule
Second parameter: Flag
Re. match only matches the start of the string. if the start of the string does not match the regular expression, the match fails. the function returns None.
match(pattern, string, flags=0)
First parameter: Rule
The second parameter indicates the string to be matched.
Third parameter: Peugeot bit, used to control the matching mode of regular expressions
Re. search matches the entire string until a match is found.
search(pattern, string, flags=0)
First parameter: Rule
The second parameter indicates the string to be matched.
Third parameter: Peugeot bit, used to control the matching mode of regular expressions
>>> Import re >>> pattern = re. compile (r 'linuxey') >>> match = pattern. match ('jb51.. net ') >>> print match <_ sre. SRE_Match object at 0x7f4e96e6160>>> print match. group () linuxeye >>> m = pattern. match ('blog .jb51.net') # match matches the beginning, not found >>> print mNone >>> m = pattern. search ('blog .jb51.net') # search matches the entire string until a match is found >>> print m <_ sre. SRE_Match object at 0x7f15abfc6b28 >>> print m. group () linuxeye
>>> M = re. match (r 'linuxey', 'jb51. net ') # No re. compile >>> print m <_ sre. SRE_Match object at 0x7f4e96e61b90 >>> print m. group () linuxeye >>> m = re. match (r 'linuxey', 'www .jb51.net') >>> print mNone
Re. split is used to split strings.
split(pattern, string, maxsplit=0)
First parameter: Rule
Second parameter: string
The third parameter: the maximum string to be separated. the default value is 0, indicating that each matching item is separated.
Instance: splits all strings.
>>> Import re >>> test_str = "1 2 3 4 5" >>> re. split (r '\ s +', test_str) ['1', '2', '3', '4', '5'] >>> re. split (r '\ s +', test_str, 2) # split the first two ['1', '2 ', '3 4 5'] >>> test_str = "1. 2. 3. 4. 5 ">>> re. split (r '\. ', test_str) ['1', '2', '3', '4', '5'] >>> re. split (r '\. ', test_str, 3) ['1', '2', '3', '4. 5']
Re. findall: find matching strings in the target string
findall(pattern, string, flags=0)
First parameter: Rule
Second parameter: Target string
But there are three parameters: You can also select items with a rule.
The returned result is a list of matched strings. If no matching string is found, a null value is returned.
>>> import re>>> test_mail = '
test03@gmail.net'>>> mail_re = re.compile(r'\w+@g....\.[a-z]{3}')>>> re.findall(mail_re,test_mail)['test01@gmail.com', 'test02@gmail.org', 'test03@gmail.net']
Re. sub replacement based on regular expressions
sub(pattern, repl, string, count=0)
First parameter: Rule
Second parameter: the replaced string
Third parameter: string
Fourth parameter: number of replicas. The default value is 0, indicating that each matching item is replaced.
>>> test = 'blog.jb51.net jb51.net'>>> test_re = re.compile(r'\.')>>> re.sub(test_re,'--',test)'blog--linuxeye--com linuxeye--com'>>> re.sub(test_re,'--',test,1)'blog--jb51.net jb51.net'