Reputation: 279
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")
Upvotes: 0
Views: 292
Reputation: 7487
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 your tblcnv
function.
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)", lambda m: tblcnv(m.group(1)), rval)
print tblcnt
print tblcnv("<table is in the place where you sit daily")
Upvotes: 1
Reputation: 19601
When defining tblcnt, you're passing tblcnv('\1'). '\1' is the string containing a single byte with value 0x01. So the result from tblcnv is that same string.
Upvotes: 2
Reputation: 3741
This line:
tblcnt = re.sub(r"<(table.*place)", tblcnv('\1'), rval)
probably doesn't do what you think it should. The call to tblcnv('\1')
returns '\1' which is the smiley face you see and then it replaces the first chunk of your string with said smiley face.
Upvotes: 2