The following is a list of several matching usages of a python regular expression, as follows:
In addition, all http://deerchao.net/tutorials/regex/regex.htm about the regular
1. Test whether the regular expression matches all or part of the string
Regex=ur "" #正则表达式if re.search (regex, subject):d o_something () else:do_anotherthing ()
2. Test whether the regular expression matches the entire string
Regex=ur "\z" #正则表达式末尾以 \z End If Re.match (Regex, subject): do_something () Else: do_anotherthing ()
3. Create a matching object and then get the matching details from that object (create an object with details about how the regex matches (part of) a string)
Regex=ur "" #正则表达式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 that matches the regular expression (get the part of a string matched by the regex)
Regex=ur "" #正则表达式match = Re.search (regex, subject) If match: result = Match.group () Else: result = ""
5. Get the substring matching the capturing group (get the part of a string matched by a capturing group)
Regex=ur "" #正则表达式match = Re.search (regex, subject) If match: result = Match.group (1) Else: result = ""
6. Get the substring matching the named group (get the part of a string matched by a named group)
Regex=ur "" #正则表达式match = Re.search (regex, subject) If Match:result = Match.group "groupname") Else:result = ""
7. Put all matching 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 matching 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:mat Ch.group ()
9. Create a regular expression object from a regular expression string (create anobject to use the same regex for many operations)
Reobj = Re.compile (regex)
10. Using the regular Expression object version of 1 (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. Usage 2 of the regular Expression object version (use Regex object for If/else branch whether a string can be matched entirely)
Reobj = Re.compile (r "\z") # Regular expression end with \z End If Reobj.match (subject): do_something () Else: do_anotherthing ()
12. Create a regular Expression object and then obtain the 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 (Exclusi VE): Atch.end () # matched Text:match.group () do_something () Else: do_anotherthing ()
13. Get the matching substring with the regular expression object ( 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. Get the substring matching the capturing group with the regular Expression object (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 the regular expression object to get the substring of the well-known group (using the 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. Get all matched substrings with the regular expression object and put into the array (use the 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 through all matches in a string)
Reobj = Re.compile (regex) for match in Reobj.finditer (subject): # Match Start:match.start () # match End (Exclusiv E): Match.end () # matched Text:match.group ()
String substitution
1. Replace all matching substrings
#用newstring替换subject中所有与正则表达式regex匹配的子串result = Re.sub (regex, newstring, subject)
2. Replace all matching 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 (use regular Expression object)
Reobj = Re.compile (regex) result = Reobj.split (subject)