How do I *display the last 6 digits of my resident registration number in Python regular expression?

Asked 2 years ago, Updated 2 years ago, 19 views

import re
ju = input("") # 123456-1234567
print(re.sub("d{7}","1******", ju))

I don't know how to put it in order to cut it to 6 digits and get the front seat...ㅜ<

python

2022-09-22 19:04

1 Answers

It is simple to use grouping when compiling regular expressions.

If you look at the example, you'll understand it simply.

I randomly opened the code...

import re
Example sentence = "123456 - 1234567"
Pattern = re.compile()
    r"(\d{6} - \d{1})\d{6}"
)

print(
    Pattern.sub("\g<1>****", example sentence)
) # Result: 123456 - 1**** 

Among the codes, brackets (...) in the above expression and "\g" in the following expression are the grouping associated grammar.

Another way to write it,

import re
Example sentence = "123456 - 1234567"
Pattern = re.compile()
    r"(\d{6}) - (\d{1})\d{6}"
)

print(
    Pattern.sub("\g<1> - \g<2>****", example sentence)
) # Result: 123456 - 1**** 


2022-09-22 19:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.