简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM