[Tutor] question regarding regular expression compile

Adam Bark adam.jtm30 at gmail.com
Wed Jan 12 15:11:42 CET 2011


On 12/01/11 13:19, Yaniga, Frank wrote:
> I am determining a regular expression that can recognize the any of 
> the following strings:
> MAT file log\20101225 deleted
> MAT file billing\20101225 deleted
> MAT file util\20101225 deleted
> MAT file carrier\20101225 deleted
> I begin by creating a regular expression object so that I can reuse it 
> in multiple operations:
> test = re.compile('MAT file
> for log, billing, util, and carrier I use an arbitrary match:
>         (log|billing|util|carrier)
> for 20101225 I use decimal digit with repetition match:
> \d{8}
> and finish with:
> delete')
> My question is how do I handle the backslash (NOTE: the match must 
> only be a backslash)?

Hi Frank,

There are two things you need to know, the first is that \ is a special 
character in re's, which I think you probably know already. Also they 
are special characters in python strings unless you use raw strings. So 
to look for the backslash in your example you need to use a raw string 
(technically you could use twice as many backslashes to escape the 
python specialness and re but it looks horrible) by starting the string 
with r"your regular expression". Then you need to use two backslashes to 
give you a backslash character. So your re should now look like this:

test = re.compile(r"MAT file (log|billing|util|carrier)\\\d{8} deleted")

HTH,
Adam.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110112/3638b7b2/attachment-0001.html>


More information about the Tutor mailing list