re Challenge: More Compact?

Tim Daneliuk tundra at tundraware.com
Mon Jul 16 19:50:02 CEST 2001


John Machin wrote:
> 
<SNIP>

(Sound of TV Audience Clapping And Yelling, "Good Answer, John")


In the situation I am using this, I don't need to known whether
the digits are in range.  I am looking at a log file and just
want to know whether a given field is in host name or IP quad
format (so I can do an reverse on the quad)...

Thanx to all who chimed in... and here I thought I understood
re construction ... boy, am I a loser ;)

> Now for the third big two-byte saving: throw away the rightmost set of
> capturing parentheses.
> ipquad   = r"^(\d{1,3}\.){3}\d{1,3}$"
> Either (a) you have no intention of retrieving parts of the matched
> string later, so the "capturing" functionality is of no use to you, or
> (b) you haven't considered this: if your input string is
> "010.020.030.040", then with your original re, groups 0 and 1 will be
> "010.020.030.040", group 2 will be "030." and group 3 will "040" --
> about as useful as a hip pocket on an athletic supporter.
> 
> OK, so now we've trimmed the text description of the re down a little,
> let's revisit the "legitimate IP address" notion. Is 999.999.999.999
> now a legitimate IP address? Last I heard, the max for each little
> doodad was 255. If so, you may not want to cr[au]ft a complicated re
> to try to enforce that. The one-liner freaks would no doubt relish a
> challenge to come up with something better than
> 
> max(map(int, your_input_string.split("."))) > 255
> 
> to use as a test *after* the uncomplicated_re.match() is successful.


-- 
------------------------------------------------------------------------------
Tim Daneliuk
tundra at tundraware.com



More information about the Python-list mailing list