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

This article uses Python 2.7 for verification.

Basics of group replacement

When you want to do a group substitution of a regular expression using re.sub (), you usually write it like this. (* Be careful to write '\' twice before group number 1)

python


re.sub(r'(hoge)', '\\1bar', 'hogefoo')
#result:
# 'hogebarfoo'

Case that causes an error

However, with this writing method, if you want to put a number after the ** group number, you will get the following error. ** **

python


# 'Third'To'number 3'Want to
re.sub(r'(No.)three', '\\13', 'No.three')
#result:
# ...
# raise error, "invalid group reference"
# sre_constants.error: invalid group reference

This is because the group number is recognized as 13 (in the above example) instead of 1.

How to respond

In such a case, you can solve it by writing as follows.

python


re.sub(r'(No.)three', '\g<1>3', 'No.three')
#result:
#number 3

Recommended Posts

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
How to identify the element with the smallest number of characters in a Python list?
Get the number of searches with a regular expression. SeleniumBasic VBA Python
[Python] How to put any number of standard inputs in a list
How to get a list of files in the same directory with python
How to get the number of digits in Python
Change the string to be replaced according to the matched string by replacing with Python regular expression
How to count the number of occurrences of each element in the list in Python with weight
Make a note of what you want to do in the future with Raspberry Pi
How to determine the existence of a selenium element in Python
How to get all the possible values in a regular expression
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
Write a script in Shell and Python to notify you in Slack when the process is finished
When you want to replace multiple characters in a string without using regular expressions in python3 series
If you want a singleton in python, think of the module as a singleton
[Python] A program that calculates the number of socks to be paired
Python Note: When you want to know the attributes of an object
[Introduction to Python] How to sort the contents of a list efficiently with list sort
How to put a line number at the beginning of a CSV file
How to write a string when there are multiple lines in python
[Introduction to Python] How to write a character string with the format function
Memorandum Regular expression When there are multiple characters in the character string that you want to separate
I want to solve the problem of memory leak when outputting a large number of images with Matplotlib
How to write regular expression patterns in Linux
I want to write to a file with Python
How to pass the execution result of a shell command in a list in Python
Feel free to write a test with nose (in the case of + gevent)
How to build an environment when you want to use python2.7 after installing Anaconda3
How to mention a user group in slack notification, how to check the id of the user group
How to write environment variables that you don't want to put on [GitHub] Python
How to count the number of elements in Django and output to a template
Visualize the timeline of the number of issues on GitHub assigned to you in Python
What you want to memorize with the basic "string manipulation" grammar of python
[Introduction to Python] How to get the index of data with a for statement
[Python3] Code that can be used when you want to cut out an image in a specific size
[Python] Precautions when finding the maximum and minimum values in a numpy array with a small number of elements
[Python] How to deal with the is instance error "is instance () arg 2 must be a type or tuple of types"
[Python3] Code that can be used when you want to change the extension of an image at once
How to deal with "^ [[A ^ [[B ^ [[C ^ [[D"] when you press the arrow keys when executing python on mac
Run the output code with tkinter, saying "A, pretending to be B" in python
How to convert / restore a string with [] in python
I want to write in Python! (2) Let's write a test
How to write a list / dictionary type of Python3
I want to work with a robot in python.
[Note] How to write QR code and description in the same image with python
[Ruby] How to replace only a part of the string matched by the regular expression?
I want to write in Python! (3) Utilize the mock
How to check in Python if one of the elements of a list is in another list
A memo of misunderstanding when trying to load the entire self-made module with Python3
When you want to plt.save in a for statement
The 15th offline real-time I tried to solve the problem of how to write with python
Get the matched string with a regular expression and reuse it when replacing on Python3
How to write offline real time I tried to solve the problem of F02 with Python
[Python] I want to make a 3D scatter plot of the epicenter with Cartopy + Matplotlib!
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
Recursively get the Excel list in a specific folder with python and write it to Excel.
How to quickly count the frequency of appearance of characters from a character string in Python?
How to know the number of GPUs from python ~ Notes on using multiprocessing with pytorch ~
[Python3] Code that can be used when you want to resize images in folder units
What seems to be a template of the standard input part of the competition pro in python3