A summary of usage instances of Python's regular expressions _python

Source: Internet
Author: User
Tags in python

Regular expression is a very useful function in Python programming, this article makes a summary of common regular expressions for everyone's reference. Specifically as follows:

One, string substitution

1. Replace all matching substrings

Replaces all substrings in the subject with the regular expression regex 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 split

1. String splitting

result = Re.split (regex, subject)

2. String splitting (using regular expression objects)

Reobj = Re.compile (regex) Result
= Reobj.split (subject)

Third, match

Several matching usages of the Python regular expression are listed below:

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 through the object

Regex=ur "..." #正则表达式
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. Gets the substring that the regular expression matches

(Get the part a string matched by the regex)

Regex=ur "..." #正则表达式
match = Re.search (regex, subject)
if match: Result
  = Match.group ()
Else:
  result = ""

5. Gets 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. Gets the substring that the named group matches

(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 the matching substrings in the string into the array

(Get an array of all regex matches in a string)

result = Re.findall (regex, subject)

8. Iterate through all matching substrings

(Iterate over all matches in a string)

For the match in Re.finditer (R) < (. *?) \s*.*?/\1> ", subject)
  # match Start:match.start ()
  # match End (exclusive): Match.end ()
  # matched Text:ma Tch.group ()

9. Creating 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. The regular Expression object version of Usage 1

(Use Regex object with If/else branch whether (part of) A string can is matched)

Reobj = Re.compile (regex)
if Reobj.search (subject):
  do_something ()
Else:
  do_anotherthing ()

11. The regular Expression object version of Usage 2

(Use Regex object for If/else branch whether a string can be matched entirely)

Reobj = Re.compile (r "\z") #正则表达式末尾以 \z End
if Reobj.match (subject):
  do_something ()
else:
  do_ Anotherthing ()

12. Create a regular Expression object, and then get the matching details through the object

(Create an object and 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. Get matching substring with 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 matched by 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. Get the substring matched by the named group with the regular expression object

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

(Use Regex object-get "an array of ' all" regex matches in a string)

Reobj = Re.compile (regex) Result
= Reobj.findall (subject)

17. Traversal of all matching substrings through regular expression objects

(Use Regex object to iterate the all matches in a string)

Reobj = Re.compile (regex) for
match in Reobj.finditer (subject):
  # Match Start:match.start ()
  # match End (exc lusive): Match.end ()
  # matched Text:match.group ()

Interested readers can start debugging this article example code, I believe there will be new gains.

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.