Re (regular expression) module
The primary function of regular expressions (regular expression) is to search for what you want to find by using a specific pattern (pattern) from a string.
First, Re.match
Re.match tries to match a pattern from the beginning of the string.
Such as: The following example matches the first word.
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 '
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.
Second, Re.search
The Re.search function finds pattern matches within a string, until the first match is found and then returns none if the string does not match.
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 '
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.
Third, 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 '-':
Import re
Text = "Jgood is a handsome boy, he's cool, clever, and so on ..."
Print re.sub (R ' \s+ ', '-', text)
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 ' [] '.
Iv. 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.
Wu, 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 '.
Liu, 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:
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 [].
Learn Django's regular expression re module