[Tutor] use of variables in re.sub

Eva Bofias 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

Thank you

Eva Bofias
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130214/99f5eaf6/attachment.html>

More information about the Tutor mailing list