0

in the program \1 should replace the first number in the text but it is not replacing can anyone help

def remove():
    r=re.compile(r'(\d{6})')
    text=pyperclip.paste()
    m=r.sub(r'\1*****',text)
    print(m)

if we have a six digit number like 252526 then it should be replaced by 2*****

Cv Nikhil
  • 25
  • 7

1 Answers1

0

You should do this instead:

import re

r = re.compile(r'(\d)(\d{5})')
text = "252526"
m = r.sub(r'\1*****', text)
print(m)

Output

2*****

The problem with your current pattern is that the capturing group refers to the whole six numbers, if you want the first only you will need to use another capturing group: hence (\d)(\d{5}). Now \1 refers to the first digit only.

Dani Mesejo
  • 43,691
  • 6
  • 29
  • 53