[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> &lt;<a href="mailto:mccredie@gmail.com">mccredie@gmail.com</a>&gt; \
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>&gt; Any common \
knowledge IP matching RE?<br><br>I don&#39;t know if there is any common knowledge \
RE, but I came up with<br>the \
following:<br><br>r&quot;((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)&quot;)
 </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),&nbsp; so I need it to be \
a regex to be matched against, and not a piece&nbsp; 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>&nbsp;&nbsp;&nbsp;&nbsp;octs = \
x.split(&quot;.&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;if len(octs) != 4: \
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return \
False<br>&nbsp;&nbsp;&nbsp;&nbsp;for oct in \
octs:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if len(oct) &gt; 1 and \
oct[0] == &quot;0&quot;:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return \
False<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if \
not 0 &lt;= int(oct) &lt; \
256:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return \
False <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;except \
ValueError:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return \
False<br>&nbsp;&nbsp;&nbsp;&nbsp;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