Home > front end >  How to replace different patterns with the same replacement?
How to replace different patterns with the same replacement?

Time:12-02

I am trying to replace multiple items using regex but am not getting the expected Output. In the below code I need to replace the phone number and the word 'hi' with "X"

txt = "Hi, my phone number is 089992654231. I am 24 years old."

def processString3(txt):
    txt = re.sub('[0-9],Hi]', 'X', txt)
    print(txt)

processString3(txt)

Expected Output - XX, my phone number is XXXXXXXXXX. I am 24 years old.

CodePudding user response:

You might find the following acceptable:

txt = "Hi, my phone number is 089992654231. I am 24 years old."

def processString3(txt):
    txt = re.sub('[0-9]{5,}|Hi', lambda m: re.sub(r'.', 'X', m.group()), txt)
    print(txt)

processString3(txt)
# XX, my phone number is XXXXXXXXXXXX. I am 24 years old.

The above logic defines a target phone number as any 5 digits or more in a row. This would exclude ages, which should never be more than 3 digits.

CodePudding user response:

Your regexp is misformed, try:

import re

def processString3(txt):
    phone_number = re.search(r'(\d{5,})', txt)
    greetings = re.search(r'(Hi)', txt)
    if phone_number:
        number_str = phone_number.group(1)
        txt = txt.replace(number_str, 'X' * len(number_str))
    if greetings:
        greeting_str = greetings.group(1)
        txt = txt.replace(greeting_str, 'X' * len(greeting_str))
    print(txt)


if __name__ == '__main__':
    txt = "Hi, my phone number is 089992654231. I am 24 years old."
    processString3(txt)

This is a temporary solution because you don't specify exactly the words you want to hide. If you give more details I can return a more efficient answer.

  • Related