[Tutor] use of variables in re.sub
eva.bofias at gmail.com
Thu Feb 14 13:57:04 CET 2013
I need to do a substitution in a regular expression that depends on a
variable. To simplify I want to be able to do this substitution:
text2='XX. AA YY. DD'
re.sub(ur"\. (AA|BB|ÇÇ","(.) \g<1>",text2)
this would give as a result:
'XX(.) AA YY. DD'
Which is exactly what I want.
But when I try to substitute AA|BB|CC for a variable I do not know how to
make it work.
I wanted to use re.compile. I tryed:
But if I try:
And I do not know how to use the variables in there. Should I use
re.compile or is there something else that I should be using
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor