As Ignacio Vasquez-Abrams suggested, you can solve your problems by passing the re.sub() function you are re.sub() . I thought a code example would explain this best, so here you go:
import re s = "fsa fad fdsa dsafasdf usa USA usa fdas adfs.f fdsa f.afda" s_pat = r'(?<=\s)(([a-zA-Z]\.)+[a-zA-Z]\.{0,1})(?=\s)' pat = re.compile(s_pat) def add_acronym_tag(match_object): s = match_object.group(0) s = s.replace('.', '').upper() return "<acronym>%s</acronym>" % s s = re.sub(pat, add_acronym_tag, s) print s
The above prints:
fsa fad fdsa dsafasdf <acronym>USA</acronym> <acronym>USA</acronym> <acronym>USA</acronym> fdas adfs.f fdsa f.afda
This way you are not actually changing the backlink because the lines are immutable. But it’s just as good: you can write a function for any processing you need, and then return whatever you want, and this is what re.sub() will insert into the final result.
Please note: you can use regular expressions inside your function; I just used the .replace() string method, because you just want to get rid of one character, and for this you do not need the full power of regular expressions.
source share