[Tutor] advice on idiomatic python and re module question

Bob Gailer bgailer at alum.rpi.edu
Mon May 17 16:38:00 EDT 2004


At 12:34 PM 5/17/2004, Andrew Fant wrote:
>First, I cannot seem to compile and use more than one regexp in a program:

You can have as many regexps as you like.

>if I use:
>
>pattern1=re.compile('^#')
>pattern2=re.compile('eth0')
>
>to define the patterns, I can use:
>
>if pattern1.match(line):
>
>to check for a comment line succesfully,but
>
>if pattern2.match(line):

Note that match starts at the beginning of the line. Hence (1) the ^ is 
superfluous and (2) 'eth0' must be at the beginning of the line to match. 
If 'eth0' is not at the beginning, the pattern should be '.*eth0'. Or you 
could use search instead of match, which looks thru the line for the pattern.

[snip]

Bob Gailer
bgailer at alum.rpi.edu
303 442 2625 home
720 938 2625 cell 




More information about the Tutor mailing list