re Challenge: More Compact?

Tim Hammerquist tim at
Mon Jul 16 06:38:01 CEST 2001

Me parece que Quinn Dunkan <quinn at> dijo:
> On Mon, 16 Jul 2001 00:58:46 GMT, Tim Hammerquist <tim at> wrote:
> >from 'Mastering Regular Expressions', p.124:
> >
> >'^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.\
> >([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])$'
> How about:
> for o in '.'.split(ip_addr):
>     assert 0 <= int(o) <= 255, 'ip octet not a byte in "%s"' % ip_addr
> Re's can be handy, but their usefulness can trick you into trying to use them
> for things for which they're not suitable.

That was the point the author was making when he said "Was it worth the
trouble?" (which was snipped).

Also, Perler's can be as over-zealous with re's as Pythonista can be
with the string module.  Sometimes re's are faster, though maybe not in
this case.  Depends on number of function calls and so many other
things.  I don't think I'll benchmark it, tho.

BTW: your solution allows ''. Is this a valid IP?  ;)

640K ought to be enough for anybody.
	-- Bill Gates (circa 1981)

More information about the Python-list mailing list