Lyesgigs
Lyesgigs

Reputation: 129

Python : different regular expressions with different substitutions

I have a couple of different regular expressions to be matched and substitued in a given text.

I am trying to do this with combined regex as follows:

regex = re.compile(r'((.*)founder(.*)|^[0-9]{4}$')

The issue is that when applying substitution on regex , i need to know which of the combined pattern is matched to use the corresponding substitution.

I'd be grateful if somone can help me to implement this !

Upvotes: 1

Views: 62

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627103

It seems you can easily avoid regex here:

def replace_em(text):
    if 'founder' in text:                   # if text contains founder
        return 'CEO'
    elif text.isdigit() and len(text) == 9: # all digits and length = 9
        return 'NUM'
    else:
        return text

print(replace_em("Some founder here")) # CEO
print(replace_em("123456789"))         # NUM
print(replace_em("Some other text"))   # Some other text

See the Python demo.

If you want to play around with regex and re.sub you may try using this code, but mind it is less efficient:

import re

def replace_em(m):
    return 'NUM' if m.group(1) else 'CEO'

regex = re.compile(r'(?s)^([0-9]{9})$|.*founder.*')

print(re.sub(regex, replace_em, "Some founder here"))
print(re.sub(regex, replace_em, "123456789"))
print(re.sub(regex, replace_em, "Some other text"))

See another Python demo

Here, (?s).*founder.*|^([0-9]{9})$ matches any string having founder in it with the first alternative (mind the (?s) makes . match any char including line break chars) and another alternative matches and captures into Group 1 a nine-digit string. The second argument is a callback method that replaces the matches based on custom logic (if Group 1 matched, replace with NUM else, with CEO).

Upvotes: 0

Rakesh
Rakesh

Reputation: 82785

You can use re.sub

Ex:

import re

s = """ if the text contains the word founder
123456789 sdfsdfsdf sdfsdf sdfsdfsdf"""

text = re.sub("founder", "CEO", s)
text = re.sub("[0-9]{9}", "NUM", text)  

print(text)

Output:

 if the text contains the word CEO
NUM sdfsdfsdf sdfsdf sdfsdfsdf

Upvotes: 1

Related Questions