[Edu-sig] self within re.sub

Bruno Vernier vernier@vc.bc.ca
Tue, 6 Feb 2001 15:58:33 -0800


Continuation...


here is a standalone python program that causes no error messages but does
not do what I expect it to do:

-----------------------------------------------

import re,rexec
r = rexec.RExec()

def dothis(r,ctag,content,otag):
   print content
   r.r_exec("""a="%s" """%content)
   return "eval to:%s "%r.r_eval('a') 


c='asdf'
a=r'(?ims)<(py|que)>(.*?)</(\1)>'
text="<py>do this</py><que>a</que>"
print re.sub(a,dothis(r,r'\1',r'\2',r'\3'),text)

------------------------------------------------

The trouble is that \1, \2, \3 are like pointers, not the actual match
while it is in the method dothis. So it does not process the content.

BUT, it I use a compiled regular expression and  rexp.sub(dothis,text) then
it does work but I lose access to r (the restricted python environment)

You could argue:  why not put r inside the dothis function.  Yes this would
work but then I cannot have make use of previous python calculations within
the same wikipage.

Bruno