On Wed, Mar 16, 2011 at 11:27 PM, Santhosh Edukulla <santhosh.eduku...@gmail.com> wrote: > Hi All, > > I have a list of regular expressions under a file(EX: inp.txt). Then, I have > another file (EX: search.txt).The task is that i have to read the list of > regular expressions from the inp.txt and then search for the same in > "search.txt" and output the matched string. > > Iam using python *"re" *module to do this. > > This works except for few regular expressions below. > > *a = '(/|\\)cmd\.com$'* > > 1. > When i read the regular expression above from the file to a variable *(EX: > a)*. as below > EX: > *a=open('inp.txt','r').readlines()[0]*
There seem to be several issues here: * The above will also give you an end-of-line character at the end of 'a'. You will need to remove that, e.g., one way is: import os a = open( 'inp.txt','r' ).readlines()[0] a = a.rstrip( os.linesep ) * If setting 'a' directly in the interpreter, the best way is to use a raw string: a = r'(/|\\)cmd\.com$' Then, both of re.match( a, '\cmd.com' ) re.match( a, '/cmd.com' ) give a match. * If reading from a file, with read()/readlines(), things should just work, i.e., if the file starts with the line: (/|\\)cmd\.com$ then, a = open( 'inp.txt','r' ).readlines()[0] a = a.rstrip( os.linesep ) should match '/cmd.com' and '\cmd.com' Regards, Gora _______________________________________________ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers