How to match literal backslashes read from a text file using regular expressions?

cricfan at gmail.com cricfan at gmail.com
Tue Jul 12 18:29:38 EDT 2005


I'm parsing a text file to extract word definitions. For example the
input text file contains the following content:

di.va.gate \'di_--v*-.ga_-t\ vb
pas.sim \'pas-*m\ adv : here and there : THROUGHOUT

I am trying to obtain words between two literal backslashes (\ .. \). I
am not able to  match words between two literal backslashes using the
regxp - re.compile(r'\\[^\\]*\\').

Here is my sample script:

import re;

#slashPattern = re.compile(re.escape(r'\\[^\\]*\\'));
pattern = r'\\[^\\]*\\'
slashPattern = re.compile(pattern);

fdr = file( "parseinput",'r');
line = fdr.readline();

while (line != ""):
    if (slashPattern.match(line)):
        print  line.rstrip()  + " <-- matches pattern " + pattern
    else:
        print  line.rstrip()  + " <-- DOES not match pattern " +
pattern
    line = fdr.readline();
    print;


----------
The output

C:\home\krishna\lang\python>python wsparsetest.py
python wsparsetest.py
di.va.gate \'di_--v*-.ga_-t\ vb                     <-- DOES not match
pattern \\[^\\]*\\
pas.sim \'pas-*m\ adv : here and there : THROUGHOUT <-- DOES not match
pattern \\[^\\]*\\
-----------

What should I be doing to match those literal backslashes? 

Thanks




More information about the Python-list mailing list