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

Question:

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.

Asked By: Alex

||

Answers:

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
Answered By: The fourth bird

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.

Answered By: JvdV

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
Answered By: Tim Biegeleisen
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.