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

George Sakkis gsakkis at rutgers.edu
Wed Jul 13 01:53:36 CEST 2005


This should give you an idea of how to go about it (needs python 2.3 or
newer):


import re
slashPattern = re.compile(r'\\(.*?)\\')

for i,line in enumerate(file("parseinput")):
    print "line", i+1,
    match = slashPattern.search(line)
    if match:
        print "matched:", match.group(1)
    else:
        print "did not match"

#===== output =======================

line 1 matched: 'di_--v*-.ga_-t
line 2 matched: 'pas-*m

#====================================


George




More information about the Python-list mailing list