Summary of usage instances of the Python Express expression

Source: Internet
Author: User
Regular expression is a very useful function in Python program design, this article makes a summary of common regular expressions, for everyone's reference. Specific as follows:

One, string substitution

1. Replace all matching substrings

Replace all substrings in the subject with regex matching the regular expression with newstring

result, Number = RE.SUBN (regex, newstring, subject)

2. Replace all matching substrings (using regular Expression objects)

Reobj = Re.compile (regex) result, Number = REOBJ.SUBN (newstring, subject)

Second, 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)

Third, matching

The following are some of the matching usages of Python regular expressions:

1. Test whether the regular expression matches all or part of the string

Regex=ur "..." #正则表达式if re.search (Regex, subject):  do_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

Regex=ur "..." #正则表达式match = Re.search (regex, subject) if match:  # match Start:match.start ()  # match End (Exclusiv E): Match.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 that the capturing group matches

(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 matched by the named group

(Get the part of a string matched by a named group) Regex=ur "..." #正则表达式match = Re.search (regex, subject) If match:  ResU lt = 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-all matches-a string) for match in Re.finditer (R "< (. *?) \s*.*?/\1> ", subject)  # match Start:match.start ()  # match End (exclusive): Match.end ()  # matched Text:ma Tch.group ()

9. Create a regular expression object from a regular expression string

(Create an object to use the same regex for many operations) Reobj = Re.compile (regex)

10. Usage 1 of the 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 (su bject):  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") #正则表达式末尾以 \z End If R Eobj.match (subject):  do_something () Else:  do_anotherthing ()

12. Create a regular Expression object and then get the matching details from that object

(Create an object with details on how to the Regex object matches (part of) a string) Reobj = Re.compile (regex) match = Reob J.search (subject) If match:  # match Start:match.start ()  # match End (exclusive): Match.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) I F match:  result = Match.group () Else:  result = ""

14. Get the substring matching the capturing group with the regular expression object

(Use Regex, object to get, part of a, string matched by a capturing group) Reobj = Re.compile (regex) match = Reobj.search (s Ubject) If match:  result = Match.group (1) Else:  result = ""

15. Use regular Expression object to get the substring matching the well-known group

(Use Regex object to get the part of a string matched by a named group) Reobj = Re.compile (regex) match = Reobj.search (subje CT) If match:  result = Match.group ("groupname") Else:  result = ""

16. Get all matched substrings with the regular expression object and put them in the 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 matching substrings through the regular expression object

(Use Regex-object to iterate-over-all matches-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 ()

Interested readers can start debugging This example code, I believe there will be a new harvest.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.