Examples of Regular Expression usage in Python, python Regular Expression

Source: Internet
Author: User

Examples of Regular Expression usage in Python, python Regular Expression

Regular Expressions are very useful functions in Python programming. This article will summarize common Regular Expressions for your reference. The details are as follows:

I. String replacement

1. Replace all matched substrings

Replace all substrings in the subject that match the regular expression regex with newstring

result, number = re.subn(regex, newstring, subject)

2. replace all matched substrings (using regular expression objects)

reobj = re.compile(regex)result, number = reobj.subn(newstring, subject)

Ii. 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)

Iii. Matching

The following lists the matching usage of Python Regular Expressions:

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 the regular expression with \ Z at the end of the if re. match (regex, subject): do_something () else: do_anotherthing ()

3. Create a matching object and obtain matching details through the object.

Regex = ur "... "# regular expression match = re. search (regex, subject) if match: # match start: match. start () # match end (exclusive): match. end () # matched text: match. group () do_something () else: do_anotherthing ()

4. Obtain 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): match.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 many 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): match.end()  # matched text: match.group()  do_something()else:  do_anotherthing()

13. Use a regular expression object to obtain matching substrings

(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()

Interested readers can debug the instance code in this article. I believe there will be new gains.


Matching usage of Python Regular Expressions

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 object and obtain the matching details (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: # match start: match. start () # match end (exclusive): atch. end () # matched text: match. group () do_something () else: do_anotherthing () 4. 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. 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. 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. use a regular expression string to create a regular expression object (re ...... remaining full text>

Python Regular Expressions

Pattern = re. compile (r "At \ s + '\ S +' \ s + \((? P <ProgramName> \ S + ):(? P <LineNumber> \ d +) \ s + (? P <ParaName> \ S +) \ s + is \ s + (? P <InCorrectValue> \ d + )")

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.