Python regex replace substring and value

Question:

I would like to replace substring(s) {with a certain format} from a string. And if no substring was found, append some string to the original string.

That might have been a bit confusing, so let me try to give an example. I want to replace AGE/age {digits} with AGE XXX. And if none is found, append NO AGE FOUND.

For example, say we have a string

He is in old age. At AGE 51, something something. At age 12 he bla bla.

This should be replaced with

He is in old age. At AGE XXX, something something. At AGE XXX he bla bla.

Another example:

He is in old age. Something something bla bla.

Will be replaced with:

He is in old age. Something something bla bla. NO AGE FOUND

This may not make sense at all, just trying to create an example similar to the situation I have.

Asked By: user1179317

||

Answers:

You can use re.subn:

import re

def redact(text):
    redacted, n_found = re.subn(r"(age|AGE)s+d+", "age XXX", text)
    return redacted if n_found else redacted + " NO AGE FOUND"

print(redact("He is in old age.  At AGE 51, something something.  At age 12 he bla bla."))
# He is in old age.  At age XXX, something something.  At age XXX he bla bla.

print(redact("He is in old age.  Something something bla bla."))
# He is in old age.  Something something bla bla. NO AGE FOUND

Specifically, re.subn returns a tuple containing the new string and the number of substitutions made.

Answered By: j1-lee
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.