[BangPypers] Regular Expressions
Gora Mohanty
gora at mimirtech.com
Thu Mar 17 07:06:58 CET 2011
On Wed, Mar 16, 2011 at 11:27 PM, Santhosh Edukulla
<santhosh.edukulla at 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
More information about the BangPypers
mailing list