[Tutor] regular expression need help

Kent Johnson kent37 at tds.net
Mon Nov 20 21:45:40 CET 2006


Hemantharaju Subbanna wrote:
>>>> a
> '\\ABC'
>>>> b
> 'THIS \\ABC'
> 
> I am looking for the expression that would tell me if
> the line has "\\" 

First you should be aware that neither of these strings contains a 
double slash. They contain a single slash which Python prints with an 
extra slash as an escape. This is how Python prints and interprets 
string literals:

In [1]: a='\\'

In [2]: a
Out[2]: '\\'

a only contains a single character, a single slash:
In [3]: len(a)
Out[3]: 1

If you explicitly print a, the escaping is not done and you see the 
plain value of the string:
In [4]: print a
\

If you want to know if a string contains a slash, just ask it using 'in'. :

In [5]: '\\' in a
Out[5]: True

In [6]: '\\' in 'THIS \\ABC'
Out[6]: True

Kent

> 
> re.match('',a)
> 
> tried to escape, python complains of syntax
> 
> Please help with the regular expression that would
> check for this double slash
> 
> Thank you
> --raju
> 
> 
> 
>  
> ____________________________________________________________________________________
> Sponsored Link
> 
> Mortgage rates near 39yr lows. 
> $310k for $999/mo. Calculate new payment! 
> www.LowerMyBills.com/lre
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 




More information about the Tutor mailing list