[Tutor] question regarding regular expression compile

Evert Rol evert.rol at gmail.com
Wed Jan 12 15:10:39 CET 2011


> 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)?

Use a raw string (prepend 'r'):

re.compile(r'MAT file (log|billing|util|carrier)\\\d{8} delete')

You'll notice you need to escape the backslash first, and then use a raw string by prepending an 'r' in front of the string, so the backslash gets interpreted correctly (or rather, not interpreted) by the regular expression.

Cheers,

  Evert



More information about the Tutor mailing list