This article summarizes 17 python regular expressions using classic instances, which are of great reference value. if you are interested, let's take a look at several matching usages of Python regular expressions, the specific content is as follows:
In addition, everything about regular expressions http://deerchao.net/tutorials/regex/regex.htm
1. test whether the regular expression matches all or part of the string.
Regex = ur "" # Regular expression if re. search (regex, subject): do_something () else: do_anotherthing ()
2. test whether the regular expression matches the entire string.
Regex = ur "\ Z" # end with \ Z at the end of the regular expression if re. match (regex, subject): do_something () else: do_anotherthing ()
3. create a matching objectAnd then obtain the matching details (Create an object with details about how the regex matches (part of) a string) through this object)
Regex = ur "" # Regular expression match = re. search (regex, subject) if match: # match start: match. start () # match end (exclusive): atch. end () # matched text: match. group () do_something () else: do_anotherthing ()
4. Get the substring matched by the regular expression (Get the part of a string matched by the regex)
Regex = ur "" # Regular expression match = re. search (regex, subject) if match: result = match. group () else: result = ""
5. obtain the substring matching the capture group.(Get the part of a string matched by a capturing group)
Regex = ur "" # Regular expression match = re. search (regex, subject) if match: result = match. group (1) else: result = ""
6. obtain the substring matched by the famous group(Get the part of a string matched by a named group)
Regex = ur "" # Regular expression match = re. search (regex, subject) if match: result = match. group "groupname") else: result = ""
7. put all matched substrings in the string into the array.(Get an array of all regex matches in a string)
result = re.findall(regex, subject)
8. traverse all matched substrings(Iterate over all matches in a string)
for match in re.finditer(r"<(.*?)\s*.*?/\1>", subject) # match start: match.start() # match end (exclusive): atch.end() # matched text: match.group()
9. create a regular expression object using a regular expression string (Create an object to use the same regex for Operation operations)
reobj = re.compile(regex)
10. Regular expression object version(Use regex object for if/else branch whether (part of) a string can be matched)
reobj = re.compile(regex)if reobj.search(subject): do_something()else: do_anotherthing()
11. Regular expression object version in usage 2(Use regex object for if/else branch whether a string can be matched entirely)
Reobj = re. compile (r "\ Z") # end with \ Z at the end of the regular expression if reobj. match (subject): do_something () else: do_anotherthing ()
12. create a regular expression object and obtain matching details through the object.(Create an object with details about how the regex object matches (part of) a string)
reobj = re.compile(regex)match = reobj.search(subject)if match: # match start: match.start() # match end (exclusive): atch.end() # matched text: match.group() do_something()else: do_anotherthing()
13. use a regular expression object to obtain a matched substring (Use regex object to get the part of a string matched by the regex)
reobj = re.compile(regex)match = reobj.search(subject)if match: result = match.group()else: result = ""
14. use a regular expression object to obtain the matched substring of the capture group(Use regex object to get the part of a string matched by a capturing group)
reobj = re.compile(regex)match = reobj.search(subject)if match: result = match.group(1)else: result = ""
15. Use a regular expression object to obtain the matched substring of a famous group (Use regex object to get the part of a string matched by a named group)
reobj = re.compile(regex)match = reobj.search(subject)if match: result = match.group("groupname")else: result = ""
16. use a regular expression object to obtain all matched substrings and put them in an array(Use regex object to get an array of all regex matches in a string)
reobj = re.compile(regex)result = reobj.findall(subject)
17. traverse all matched substrings through the regular expression object(Use regex object to iterate over all matches in a string)
reobj = re.compile(regex)for match in reobj.finditer(subject): # match start: match.start() # match end (exclusive): match.end() # matched text: match.group()
String replacement
1. replace all matched substrings
# Use newstring to replace all substrings in the subject that match the regular expression regex. result = re. sub (regex, newstring, subject)
2. replace all matched substrings (using regular expression objects)
reobj = re.compile(regex)result = reobj.sub(newstring, subject)
String splitting
1. string splitting
result = re.split(regex, subject)
2. string splitting (using regular expression objects)
reobj = re.compile(regex)result = reobj.split(subject)