Python string replacement method using regular expressions

Source: Internet
Author: User
Python regular expressions are often used to replace strings. This article mainly introduces how to replace strings with Python regular expressions, which has some reference value. For more information, see. Python regular expressions are often used to replace strings. This article mainly introduces how to replace strings with Python regular expressions, which has some reference value. For more information, see.

Python regular expressions are often used to replace strings. Many people do not know how to solve this problem. The code below tells you that the problem is extremely simple and I hope you will gain some benefits.

1. replace all matched substrings with newstring to replace all substrings in the subject that match the regular expression regex.

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

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

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

Python string splitting

reresult = re.split(regex, subject)

String splitting (using regular expression objects)

rereobj = re.compile(regex) result = reobj.split(subject)

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" # the regular expression ends with \ Z at the end.

if re.match(regex, subject): do_something() else: do_anotherthing()

3. create a matching object and obtain the matching details through this 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()

The above is a detailed description of the Python regular expression in string replacement. I hope it will be helpful to everyone's learning, and I hope you can support your own home.

The above is the detailed content of the string replacement method using a regular expression in Python. For more information, see other related articles in the first PHP community!

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.