In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.