Validated with Python 3.5.2.
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)
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")
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.
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))
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.
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.)
https://docs.python.jp/3/library/re.html
Recommended Posts