I want to find keywords (the keys will be dynamic) and replace them in a specific format. For example: this data
keys = ["cat", "dog", "mouse"] text = "Cat dog cat cloud miracle DOG MouSE"
had to convert to
converted_text = "[Cat](cat) [dog](dog) [cat](cat) cloud miracle [DOG](dog) [MouSE](mouse)"
Here is my code:
keys = "cat|dog|mouse" p = re.compile(u'\\b(?iu)(?P<name>(%s))\\b' % keys) converted_text = re.sub(p, '[\g<name>](\g<name>)', text)
And this works fine, only I cannot convert the last parameter to lowercase. This translates as follows:
converted_text = "[Cat](cat) [dog](dog) [cat](cat) cloud miracle [DOG](dog) [MouSE](mouse)"
how can i convert the last parameter to lowercase? it seems that python cannot compile the \ L character.
python regex
jargalan
source share