Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python regex replace substring and value

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading