re Challenge: More Compact?

Tim Daneliuk tundra at
Mon Jul 16 13:50:02 EDT 2001

John Machin wrote:

(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
> "", then with your original re, groups 0 and 1 will be
> "", 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

More information about the Python-list mailing list