[prev in list] [next in list] [prev in thread] [next in thread]
List: python-list
Subject: Re: re question
From: "Dan Bar Dov" <bardov () gmail ! com>
Date: 2007-09-21 5:31:32
Message-ID: d6944c490709202231o49eb43bapa7ab6932698a5a5 () mail ! gmail ! com
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
On 9/20/07, Matt McCredie <mccredie@gmail.com> 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
>
[Attachment #5 (text/html)]
<br><br><div><span class="gmail_quote">On 9/20/07, <b class="gmail_sendername">Matt \
McCredie</b> <<a href="mailto:mccredie@gmail.com">mccredie@gmail.com</a>> \
wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, \
204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> <br>> Any common \
knowledge IP matching RE?<br><br>I don't know if there is any common knowledge \
RE, but I came up with<br>the \
following:<br><br>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)")
</blockquote><div><br>Thank you. I will use that! <br></div><blockquote \
class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt \
0pt 0.8ex; padding-left: 1ex;"><br>I generally discourage people from using REs. I \
think the folowing is <br>much easier to read:</blockquote><div><br>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 .<br>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. <br><br>Dan \
<br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, \
204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">def \
isip(x):<br> octs = \
x.split(".")<br> if len(octs) != 4: \
<br> return \
False<br> for oct in \
octs:<br> if len(oct) > 1 and \
oct[0] == "0":<br> return \
False<br> try:<br> if \
not 0 <= int(oct) < \
256:<br> return \
False <br> except \
ValueError:<br> return \
False<br> return True<br><br>Both solutions seem to work, \
though I used a small set of test cases.<br>Others may have better \
suggestions.<br><br>Matt<br></blockquote> </div><br>
--
http://mail.python.org/mailman/listinfo/python-list
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic