i am not able to figure out why the below two print statements gives different
results. can any one please explain me? i am just giving a small sample example. in the first print statement table is getting replaced by a special character and the second one gives correct one.
import re
def tblcnv( str ):
rtval = re.sub("table", "chair", str)
return rtval
rval = "<table is in the place where you sit daily "
tblcnt = re.sub(r"<(table.*place)", tblcnv('\1'), rval)
print tblcnt
print tblcnv("<table is in the place where you sit daily")
According to the re.sub manual it takes a function which “is called for every non-overlapping occurrence of pattern”. As those occurrences are actually match objects you are best of using a simple lambda expression which extracts the
group(1)of the match objects and passes it to yourtblcnvfunction.