Get the matched string with a regular expression and reuse it when replacing on Python3

1. What you want to do

I want to use the string matched by Python regular expression replacement for replacement. I forget it every time, so make a note

2. For example?

Suppose you have a list like this in English analysis.

List with English words

sentence = ['During', 'this', 'time', ',', 'many', 'chatterbots', 'were', 'written', 'including',
            'PARRY', ',', 'Racter', ',', 'and', 'Jabberwacky', '.']

When you want to make this a string (of the whole sentence), the first thing you can think of is join.

Try joining with a space delimiter

' '.join(sentence)
#During this time , many chatterbots were written including PARRY , Racter , and Jabberwacky .

Yes ... As you might have guessed, there is a half-width space ** in front of the . or , **. I was in trouble Then what to do ... That's right. Replace with a regular expression! But how do you replace it with , or .?

3. Reuse the matched string at the replacement destination

Let's do it for the time being.

Correct the text in English

import re

bad = ' '.join(sentence)
fixed = re.sub(r' ([,.])', r'\1', bad)
# r' [,.]' :Half-width space+「,」「.Any of



During this time, many chatterbots were written including PARRY, Racter, and Jabberwacky.


4. A little commentary

When using re.sub In the pattern character string of the first argument, enclose the part you want to reuse in () Specify the number () you want to use for the part you want to replace the character string of the second argument, ": \ 1 if it is the first". Based on this, I will write some examples.

bad = 'including PARRY , Racter , and Jabberwacky .'

#The one from earlier
re.sub(r' ([,.])', r'\1', bad)
Out[4]: 'including PARRY, Racter, and Jabberwacky.'

# 「(The location is different
re.sub(r'( [,.])', r'\1', bad)
Out[5]: 'including PARRY , Racter , and Jabberwacky .' # == bad

# 「()Try to enclose the space with
re.sub(r'( )([,.])', r'\1', bad)
Out[6]: 'including PARRY  Racter  and Jabberwacky '

#Change the number to use
re.sub(r'( )([,.])', r'\2', bad)
Out[7]: 'including PARRY, Racter, and Jabberwacky.'

#Try to connect and use
re.sub(r'( )([,.])', r'\1\2', bad)
Out[8]: 'including PARRY , Racter , and Jabberwacky .' # == bad

#How about the opposite
re.sub(r'( )([,.])', r'\2\1', bad)
Out[9]: 'including PARRY,  Racter,  and Jabberwacky. ' # != bad

#Try playing
re.sub(r'( )([,.])', r'\1 Hoge\2', bad)
Out[12]: 'including PARRY Hoge,Racter Hoge,and Jabberwacky Hoge.'


Be sure to prefix '\ num' with r. Additional notes Good example r'\ 1' Bad example '\ 1'

Postscript (2017/01/19)

If you add a backslash \, it will work without the r.

*** Thank you to shiracamus for letting us know in the comments. *** ***

In the case of '\ num', it is necessary to add r, but if it is '\\ num', it is not necessary to add r. r means raw (raw = raw, raw) and is a raw (raw) string that does not treat backslashes as escape characters.

>>> import re
>>> bad = 'including PARRY , Racter , and Jabberwacky .'
>>> re.sub(' ([,.])', '\\1', bad)
'including PARRY, Racter, and Jabberwacky.'

If you have any other questions, please let us know in the comments.

Recommended Posts

Get the matched string with a regular expression and reuse it when replacing on Python3
Change the string to be replaced according to the matched string by replacing with Python regular expression
Get the number of searches with a regular expression. SeleniumBasic VBA Python
[Python] A function that searches the entire string with a regular expression and retrieves all matching strings.
Make one repeating string with a Python regular expression.
Determine if a string is a time with a python regular expression
String replacement with Python regular expression
Recursively get the Excel list in a specific folder with python and write it to Excel.
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
A memo with Python2.7 and Python3 on CentOS
Deploy a Python app on Google App Engine and integrate it with GitHub
[Personal memo] Get data on the Web and make it a DataFrame
[Python] Get the files in a folder with Python
Decrypt a string encrypted on iOS with Python
Make a thermometer with Raspberry Pi and make it visible on the browser Part 3
Make a Python program a daemon and run it automatically when the OS starts
Get the stock price of a Japanese company with Python and make a graph
Use python on Raspberry Pi 3 and turn on the LED when it gets dark!
Install selenium on Mac and try it with python
Make a breakpoint on the c layer with python
Get comments on youtube Live with [python] and [pytchat]!
When writing an if statement with a regular expression
Get mail from Gmail and label it with Python3
[Python3] Take a screenshot of a web page on the server and crop it further
[CGI] Run the Python program on the server with Vue.js + axios and get the output data
[Ruby] How to replace only a part of the string matched by the regular expression?
Until you create a machine learning environment with Python on Windows 7 and run it
Temporarily save a Python object and reuse it in another Python
Associate Python Enum with a function and make it Callable
Get a Python web page, character encode it, and display it
Get rid of dirty data with Python and regular expressions
Create a deploy script with fabric and cuisine and reuse it
Regular expression manipulation with Python
Make a BLE thermometer and get the temperature with Pythonista3
Put Ubuntu in Raspi, put Docker on it, and control GPIO with python from the container
How to write when you want to put a number after the group number to be replaced with a regular expression in re.sub of Python
Find the white Christmas rate by prefecture with Python and map it to a map of Japan
Life game with Python [I made it] (on the terminal & Tkinter)
How is the progress? Let's get on with the boom ?? in Python
Control the motor with a motor driver using python on Raspberry Pi 3!
Get started with the Python framework Django on Mac OS X
How to get all the possible values in a regular expression
Build a 64-bit Python 2.7 environment with TDM-GCC and MinGW-w64 on Windows 7
[Introduction to Python] How to split a character string with the split function
A memorandum when I tried to get it automatically with selenium
Build a Python environment on your Mac with Anaconda and PyCharm
A python regular expression, str and unicode that are sober and addictive
Error and solution when installing python3 with homebrew on mac (catalina 10.15)
Solve the Python knapsack problem with a branch and bound method
Install lp_solve on Mac OS X and call it with python.
Get the formula in an excel file as a string in Python
Get the number of readers of a treatise on Mendeley in Python
Get the weather with Python requests
Get the weather with Python requests 2
Python --Get bitcoin rate BTC / JPY from bitflyer at regular intervals and save it to a file
What to do if you get a "Wrong Python Platform" warning when using Python with the NetBeans IDE
I ran GhostScript with python, split the PDF into pages, and converted it to a JPEG image.
Use AWS lambda to scrape the news and notify LINE of updates on a regular basis [python]
How to input a character string in Python and output it as it is or in the opposite direction.
Upload data to s3 of aws with a command and update it, and delete the used data (on the way)
Get the width of the div on the server side with Selenium + PhantomJS + Python