Examples of common python Regular Expressions and python instances
Transfer ~ The following lists the matching usage of Python Regular Expressions:
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 ): & Nbsp; do_something () Else: & Nbsp; do_anotherthing () |
3. Create a matching object and obtain the matching details through the object (Create an object with details about how the regex matches (part of) a string)
Regex = ur "" # Regular Expression Match = re. search (regex, subject) If match: & Nbsp; # match start: match. start () & Nbsp; # match end (exclusive): atch. end () & Nbsp; # matched text: match. group () & Nbsp; do_something () Else: & Nbsp; 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: & Nbsp; result = match. group () Else: & Nbsp; result = "" |
5. Get the part of a string matched by a capturing group)
Regex = ur "" # Regular Expression Match = re. search (regex, subject) If match: & Nbsp; result = match. group (1) Else: & Nbsp; result = "" |
6. 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) & Nbsp; # match start: match. start () & Nbsp; # match end (exclusive): atch. end () & Nbsp; # matched text: match. group () |
9. Create a regular expression object (Create an object to use the same regex for each operations)
Reobj = re. compile (regex) |
10. use regex object for if/else branch whether (part of) a string can be matched)
Reobj = re. compile (regex) If reobj. search (subject ): & Nbsp; do_something () Else: & Nbsp; do_anotherthing () |
11. Regular Expression object version (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 ): & Nbsp; do_something () Else: & Nbsp; 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: & Nbsp; # match start: match. start () & Nbsp; # match end (exclusive): atch. end () & Nbsp; # matched text: match. group () & Nbsp; do_something () Else: & Nbsp; 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: & Nbsp; result = match. group () Else: & Nbsp; 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: & Nbsp; result = match. group (1) Else: & Nbsp; 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: & Nbsp; result = match. group ("groupname ") Else: & Nbsp; 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 ): & Nbsp; # match start: match. start () & Nbsp; # match end (exclusive): match. end () & Nbsp; # matched text: match. group () |
String replacement
1. Replace all matched substrings
# Replace all substrings in the subject with the regular expression regex with newstring 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)