Re.match
Re.match tries to match a pattern from the beginning of the string, such as: The following example matches the first word.
Copy Code code as follows:
Import re
Text = "Jgood is a handsome boy, it is cool, clever, and".
m = Re.match (r "(\w+) \s", text)
If M:
Print M.group (0), ' \ n ', M.group (1)
Else
print ' not match '
Re.match's function prototype is: Re.match (pattern, string, flags)
The first argument is a regular expression, which is "(\w+) \s" and returns a match if the match succeeds, otherwise a none is returned;
The second parameter represents the string to match;
The third parameter is the Peugeot bit, which controls how regular expressions are matched, such as whether case sensitive, multiline matching, and so on.
Re.search
The Re.search function finds pattern matches within a string, only to find the first match and then returns, and none if the string does not match.
Copy Code code as follows:
Import re
Text = "Jgood is a handsome boy, it is cool, clever, and".
m = Re.search (R ' \shan (ds) ome\s ', text)
If M:
Print M.group (0), M.group (1)
Else
print ' Not search '
Re.search's function prototype is: Re.search (pattern, string, flags)
Each parameter has the same meaning as Re.match.
The difference between Re.match and Re.search: Re.match matches only the beginning of a string, if the string does not start with a regular expression, the match fails, the function returns none, and Re.search matches the entire string until a match is found.
Re.sub
Re.sub is used to replace a match in a string. The following example replaces a space in a string with '-':
Copy Code code as follows:
Import re
Text = "Jgood is a handsome boy, it is cool, clever, and".
Print re.sub (R ' \s+ ', '-', text)
Re.sub's function prototype is: re.sub (Pattern, REPL, string, count)
The second function is the replacement string, in this case '-'
The fourth parameter refers to the number of replacements. The default is 0, which means that each match is replaced.
Re.sub also allows the use of functions to perform complex processing of the substitution of matches. For example: Re.sub (R ' \s ', Lambda m: ' [' + m.group (0) + '] ', text, 0); "Replace the space in the string with ' [] '.
Re.split
You can use Re.split to split strings, such as: Re.split (R ' \s+ ', text), and split the string into a single word list.
Re.findall
Re.findall can get all the matching strings in the string. such as: Re.findall (R ' \w*oo\w* ', text); Gets all the words in the string that contain ' oo '.
Re.compile
You can compile a regular expression into a regular expression object. Regular expressions that are often used can be compiled into regular expression objects, which can increase the efficiency of a certain type. The following is an example of a regular expression object:
Copy Code code as follows:
Import re
Text = "Jgood is a handsome boy, it is cool, clever, and".
Regex = Re.compile (R ' \w*oo\w* ')
Print Regex.findall (text) #查找所有包含 the word ' oo '
Print regex.sub (lambda m: ' [' + m.group (0) + '], text) #将字符串中含有 ' oo ' words are enclosed in [].
For more detailed information, you can refer to the Python manual.