A simple example of common python regular usage

Source: Internet
Author: User
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):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 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:mat Ch.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. 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)

The above common python regular usage of the simple example is the small part of the whole content to share to everyone, I hope to give you a reference, but also hope that we support the script house a lot.

  • Related Article

    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.