Mar-24-2019, 07:35 AM
You can pass a callable to
to replace substrings that were found.
I think, you could adopt the following code for you needs:
re.sub
. This callable should take a regexp match object and return a string. The latter is used to replace substrings that were found.
I think, you could adopt the following code for you needs:
def match_processor(m): print("Hey, I am match processor... ") print("Regexp engine found a substring: ", m.string[slice(*m.span())]) print("This substring should be replaced with another one ... and I can do this!") replacement = '-' + m.string[slice(*m.span())] + '-' return replacement list = ['<p>Material blabla ;<lb/>Dimension blabla ;<lb/></p>','<p>Sideral blublu ;<lb/>Sticky Gonzalez blibli;<lb/>Reductio ad absurdum bloblo</p>'] import re for member in list: output = re.sub(r"<lb/>\s?[A-Z]", match_processor, member) print(output)