re.compile and very specific searches

John Machin sjmachin at lexicon.net
Fri Feb 18 15:33:30 EST 2005


Diez B. Roggisch wrote:


> So I'd suggest you dump re and do it like this:
>
> address = "192.168.1.1"
>
> def validate_ip4(address):
>     digits = address.split(".")
>     if len(digits) == 4:
>         for d in digits:
>             if int(d) < 0 or int(d) > 255:
>                   return False
>     return True
>

The OP wanted to "find" IP addresses -- unclear whether re.search or
re.match is required. Your solution doesn't address the search case.
For the match case, it needs some augmentation. It will fall apart if
presented with something like "..." or "comp.lang.python.announce". AND
while I'm at it ... in the event of a valid string of digits, it will
evaluate int(d) twice, rather unnecessarily & uglily.

So: match case:

! for s in strings_possibly_containing_digits:
! #   if not(s.isdigit() and 0 <= int(s) <= 255): # prettier, but test
on zero is now redundant
!     if not s.isdigit() or int(s) > 255:

and the search case: DON'T dump re; it can find highly probable
candidates (using a regexp like the OP's original or yours) a damn
sight faster than anything else this side of C or Pyrex. Then you
validate the result, with a cut-down validator that relies on the fact
that there are 4 segments and they contain only digits:

! # no need to test length == 4
! for s in address.split('.'):
!     if int(s) > 255:

HTH,
John




More information about the Python-list mailing list