简体   繁体   中英

Using regex to match 10th to 15th digits in a long integer and replace with *

I have a input string 12345678901234567890 and I want to use re.sub to match 10th digits to 15th digits in the above string, and replace with * so the desired output will need to be 1234567890*****67890.

import re

string="12345678901234567890"
out = re.sub(".{10}\d{5}.{5}","*",string)

print(out)

Above is my current code, which is not working as expected. Does anyone has an idea on this? Any help would be appreciated.

You can capture 10 digits and match the next 5 digits.

^(\d{10})\d{5}

Then replace with group 1 using \1 and *****

Regex demo

import re

string="12345678901234567890"
out = re.sub(r"^(\d{10})\d{5}",r"\1*****",string)

print(out)

Output

1234567890*****67890

My two cents, using PyPi's regex module with zero-width lookbehind:

import regex as re

s_in = '12345678901234567890'
s_out = re.sub(r'(?<=^\d{10,14})\d(?=\d*$)', '*', s_in)

print(s_out)

Prints:

1234567890*****67890

I suppose the lookahead is not needed, but since you checking for digits anyway, why not assert that the whole string is made of digits.

Note: This will match digits no matter the lenght of the string.

If your input would always have the same width, just use a substring operation:

string = "12345678901234567890"
output = string[0:10] + "*****" + string[15:]
print(output)  # 1234567890*****67890

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM