Change the string to be replaced according to the matched string by replacing with Python regular expression

environment

Validated with Python 3.5.2.

Usage scene

When replacing with a regular expression, if you want to change the character string to be replaced according to the matched character string

As an example, you can do the following.

(I think it is possible to perform more complicated processing other than the above example)

Method

Use re.sub (), which is used for replacement with a normal regular expression, and a function that implements the processing you want to apply to the matched character string.

Specifically, it is re.sub (pattern, function, character string to be searched). If the argument of the function called above is matchobj, you can use the character string that matches the pattern by setting matchobj.group (0) in the function.

def replace(matchobj):
    s = matchobj.group(0)
    #What you want to do

re.sub(`hoge`, replace, "sampletext")

Implementation example

In the example below, we prepared a function toKansuji () that converts numbers to Chinese numerals and a function fixTax () that changes 5% consumption tax to 8%, and matched them in re.sub (). It is replaced with the result of applying the above function to the numbers.

code

sample.py


import re

def toKansuji(matchobj):
    a = ["one", "Ten thousand", "Billion", "Trillion", "Kyo"]
    b = ["one", "Ten", "hundred", "thousand"]
    c = ("", "one", "two", "three", "four", "Five", "Six", "Seven", "Eight", "Nine")
    s = matchobj.group(0)
    l = [int(i) for i in s]
    result = ""
    length = len(l)
    for i in range(length):
        n = l[length-1-i]
        if i%4 == 0 and i//4 > 0: 
            az = 1
            for j in range(4):
                if l[length-1-i-j] != 0: az = 0
            if az == 0: result = a[i//4] + result
        if n != 0:
            if i%4 != 0:
                result = b[i%4] + result
                if n != 1: result = c[n] + result
            else:
                result = c[n] + result
    return result

def fixTax(matchobj):
    d = int(matchobj.group(0))
    d = int(d / 1.05 * 1.08)
    return str(d)

if __name__ == '__main__':
    s1 = "315 yen including tax"
    print("Consumption tax 5%:", s1)
    print("Consumption tax 8%:", re.sub('\d+', fixTax, s1))

    s2 = "IPv4 is 4294967296"
    print("Numbers:", s2)
    print("Chinese numeral:", re.sub('\d{2,}', toKansuji, s2))

Execution result

Consumption tax 5%:315 yen including tax
Consumption tax 8%:324 yen including tax
Numbers:IPv4 is 4294967296
Chinese numeral:IPv4 is 492,494,967,296

The top two lines replace the consumption tax from 5% to 8%, and the bottom two lines replace the numbers with Chinese numerals.

Other

If you group using () in the pattern, you can also use the matched character string in that group by setting matchobj.group (n) in the function.

(Also, since both of the functions prepared this time are prepared for operation check, they may not return accurate values for all inputs.)

reference

https://docs.python.jp/3/library/re.html

Recommended Posts

Change the string to be replaced according to the matched string by replacing with Python regular expression
Get the matched string with a regular expression and reuse it when replacing on Python3
String replacement with Python regular expression
[Ruby] How to replace only a part of the string matched by the regular expression?
Starting with Python 3.10, the form returned by inspect.signature () seems to be based on typing.get_type_hints ().
Regular expression manipulation with Python
Determine if a string is a time with a python regular expression
How to switch the configuration file to be read by Python
[Introduction to Python] How to split a character string with the split function
[Python] A function that searches the entire string with a regular expression and retrieves all matching strings.
Have python check if the string can be converted / converted to int
[Python] Change the alphabet to numbers
Get the number of searches with a regular expression. SeleniumBasic VBA Python
[Introduction to Python] How to write a character string with the format function
Address to the bug that node.surface cannot be obtained with python3 + mecab
Change Python 64bit environment to 32bit environment with Anaconda
The road to compiling to Python 3 with Thrift
Convert the world time zone time string to Japan time without calculating the time difference with python.
What you want to memorize with the basic "string manipulation" grammar of python
Python constants like None (according to the reference)
[Python] How to change the date format (display format)
The easiest way to synthesize speech with python
Try to solve the man-machine chart with Python
Specify the Python executable to use with virtualenv
Say hello to the world with Python with IntelliJ
The easiest way to use OpenCV with python
How to erase the characters output by Python
Introduction to Python with Atom (on the way)
Run the output code with tkinter, saying "A, pretending to be B" in python
I tried to automatically generate a character string to be input to Mr. Adjustment with Python
Determine if the member should be set to the default string with just the or operator
Try to create a waveform (audio spectrum) that moves according to the sound with python
How to convert / restore a string with [] in python
Try to solve the programming challenge book with python3
How to delete the specified string with the sed command! !! !!
[Introduction to Python] How to iterate with the range function?
0 Convert unfilled date to datetime type with regular expression
Try to solve the internship assignment problem with Python
The first algorithm to learn with Python: FizzBuzz problem
I tried to touch the CSV file with Python
Change IP settings to ACL of conoha with python
Destroy the intermediate expression of the sweep method with Python
Change the movement by changing the combo box with tkinter
I tried to solve the soma cube with python
[Python] How to specify the download location with youtube-dl
Read the xml file by referring to the Python tutorial
Convert the image in .zip to PDF with Python
I want to inherit to the back with python dataclass
Shift the alphabet string by N characters in Python
Specify MinGW as the compiler to use with Python
I tried to solve the problem with Python Vol.1
[Python] How to rewrite the table style with python-pptx [python-pptx]
Display / update the graph according to the input with PySimpleGui
I tried to summarize the string operations of Python
I made a class to get the analysis result by MeCab in ndarray with python
How to calculate space background X-ray radiation (CXB) with python by specifying the flux range
Try to import to the database by manipulating ShapeFile of national land numerical information with Python