re question

2007-09-19 Thread Dan Bar Dov
I'm trying to construct a regular expression to match valid IP address,
without leading zeroes (i.e
1.2.3.4, 254.10.0.0, but not 324.1.1.1, nor 010.10.10.1)

This is what I come up with, and it does not work.

r'(^[12]?\d{0,2}\.){3,3}[12]?\d{0,2}'

What am I doing wrong?
Any common knowledge IP matching RE?

Thanks,
Dan
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: re question

2007-09-20 Thread Dan Bar Dov
On 9/20/07, Matt McCredie <[EMAIL PROTECTED]> wrote:
>
>
> > Any common knowledge IP matching RE?
>
> I don't know if there is any common knowledge RE, but I came up with
> the following:
>
>
> r"((1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.){3}(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)")


Thank you. I will use that!

>
> I generally discourage people from using REs. I think the folowing is
> much easier to read:


Well, I need the regex to be used withing a parser (yeanpypa or
ZestyParser),  so I need it to be a regex to be matched against, and not a
piece  of code .
I could probably use a more simplistic re that does not check numeric
validity, and do that later, but your re would do both matching and
validation in one step.

Dan

def isip(x):
> octs = x.split(".")
> if len(octs) != 4:
> return False
> for oct in octs:
> if len(oct) > 1 and oct[0] == "0":
> return False
> try:
> if not 0 <= int(oct) < 256:
> return False
> except ValueError:
> return False
> return True
>
> Both solutions seem to work, though I used a small set of test cases.
> Others may have better suggestions.
>
> Matt
>
-- 
http://mail.python.org/mailman/listinfo/python-list