Evaluate my first python script, please

Duncan Booth duncan.booth at invalid.invalid
Fri Mar 5 08:38:51 EST 2010


Jean-Michel Pichavant <jeanmichel at sequans.com> wrote:

> You've already been given good advices.
> Unlike some of those, I don't think using regexp an issue in any way. 
> Yes they are not readable, for sure, I 100% agree to that statement, but 
> you can make them readable very easily.
> 
> # not readable
> match = re.search('\d+\.\d+\.\d+\.\d+\s+(\S+)', line)
> 
> 
> # readable
> match = re.search('\d+\.\d+\.\d+\.\d+\s+(\S+)', line) # match 
> '192.168.200.1   (foo123)'
> 
> I gladly understand ppl that are not willing to use regexp (or as last 
> resort), but for a perl guy, that should not be an issue.

The problem with your comment is that just by existing it means people will 
be misled into thinking the regex is being used to match strings like
  '192.168.200.1   (foo123)'
when in fact the OP is expecting to match strings like
  '192.168.200.1   foo123'

i.e. you may mislead some people into thinking the parentheses are part of 
what is being matched when they're actually grouping within the regex.

Another problem with either regular expression line is that it makes it 
less easy to see that it doesn't do what the OP wanted. /etc/hosts contains  
lines with an IP address followed by multiple hostnames. It may also 
contain comments either as complete lines or at the end of lines. The code 
given will miss hostnames after the first and will include hostnames in 
commented out lines.

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list