Re.match
Re.match tries to match a pattern from the beginning of the string, such as: The following example matches the first word.
The code is as follows:
Import re
Text = "Jgood is a handsome boy, he's cool, clever, and so on ..."
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 parameter is a regular expression, here is "(\w+) \s", if the match succeeds, returns a match, otherwise returns a none;
The second parameter represents the string to match;
The third parameter is the Peugeot bit, which controls how regular expressions are matched, such as case sensitivity, multiline matching, and so on.
Re.search
The Re.search function finds a pattern match within the string, only to find the first match and then returns if the string does not match, or none.
The code is as follows:
Import re
Text = "Jgood is a handsome boy, he's cool, clever, and so on ..."
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 the string, if the string starts not conforming to the regular expression, the match fails, the function returns none, and the Re.search matches the entire string until a match is found.
Re.sub
The re.sub is used to replace a match in a string. The following example replaces a space in a string with a '-':
The code is as follows:
Import re
Text = "Jgood is a handsome boy, he's cool, clever, and so on ..."
Print re.sub (R ' \s+ ', '-', text)
Re.sub's function prototype is: re.sub (Pattern, REPL, string, count)
Where the second function is the replaced 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 for complex processing of replacements for matches using functions. such as: 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 a string, such as: Re.split (R ' \s+ ', text), and divide the string into a word list by space.
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. It is possible to compile regular expressions that are often used as regular expression objects, which can improve some efficiency. Here is an example of a regular expression object:
The code is as follows:
Import re
Text = "Jgood is a handsome boy, he's cool, clever, and so on ..."
Regex = Re.compile (R ' \w*oo\w* ')
Print Regex.findall (text) #查找所有包含 ' oo ' word
Print regex.sub (lambda m: ' [' + m.group (0) + '] ', text) #将字符串中含有 ' oo ' words are enclosed in [].
For more detailed information, refer to the Python manual.