Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use regular expressions to find and replace text in Python

Shulou Source: shulou.com Published: 2022-06-02 11:17:02 09月25日 Update

This article introduces how to use regular expressions to find and replace text in Python. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

1. Demand

We want to find and replace the text in the string.

2. Solution

For simple text patterns, use str.replace ().

For example:

Text='mark, handsome guy, 18183 handsome, mark'print (text.replace ('1819)) print (text)

Running result:

Mark, handsome guy, 19193 handsome, mark

Mark, handsome guy, 18183 handsome, mark

For more complex patterns, you can use the sub () function in the re module.

Example: change the date format from "11Universe 28DB 2018" to "2018-11-28"

Import retext=' today is: 11/28/2018'print (re.sub (re.sub (r'(\ d +) / (\ d +) / (\ d +)', r'\ 3 -\ 1 -\ 2) print (text))

Results:

Today is: 2018-11-28

Today is: 1128 Compact 2018.

The first parameter of sub () is the pattern to match, and the second parameter is the pattern to be replaced. A backslash such as "3" plus a number indicates the number of the captured group in the pattern.

If you plan to perform repeated replacements with the same pattern, consider compiling the schema first for better performance.

Example:

Import retext=' today is: 11/28/2018'datepat=re.compile (r'(\ d +) / (\ d +) / (\ d +)') print (datepat.sub (r'\ 3 -\ 1 -\ 2)) print (text)

Results:

Today is: 2018-11-28

Today is: 1128 Compact 2018.

For more complex cases, you can specify a replacement callback function.

Example:

Import refrom calendar import month_abbrtext=' today is: 11/28/2018'datepat=re.compile (r'(\ d +) / (\ d +) / (\ d +)') def change_date (m): mon_name=month_ ABBR [int (m.group (1))] return'{} {} '.format (m.group (3), mon_name,m.group (2)) print (datepat.sub (change_date,text)) print (text)

Results:

Today is: 2018 Nov 28

Today is: 1128 Compact 2018.

The input parameter to the replacement callback function is a matching object, returned by match () and find (). Use the .group () method to extract specific parts of the match. This function returns the replaced text.

In addition to getting the replaced text, if you want to know how many substitutions have been completed, you can use re.subn ().

Example:

Import retext=' today is: 1128 shock 2018, yesterday was 11/27/2018'datepat=re.compile (r'(\ d +) / (\ d +) / (\ d +)') new_text,n=datepat.subn (r'\ 3 -\ 1 -\ 2) print (text) print (new_text) print (n)

Results:

Today it is 1128max 2018, and yesterday it was 11max 27max 2018.

Today is: 2018-11-28, yesterday is 2018-11-27

On how to use regular expressions in Python to find and replace text operations to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Patterns text results functions parameters handsome regular expressions complexity content instances more examples help good same interest characters strings objects Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux macOS MySQL vpn OPPO Reno