Hi, I've needed to validate user input to ensure it is a validly formatted IPv4 address.
The one I came up with before I looked around was:
/(^(\d{1,3}\.){3})\d{1,3}$/
The double brackets are to ensure the ^ stands for 1 triplet, not all three of them such as (^\d{1,3}\.){3} which could never occur
Is there anything failsafe yet more efficient than this?
After hunting around I discovered suprisingly that many mail programs and even the LRP favour using this regex:
/^\d+\.\d+\.\d+\.\d+$/
Which will fail on things like 1111.0.0.0
As a side note, I'm sure there is a way to compare two variables to the same regex in one neat expression rather than
if ($var1 =~ /$regex/&&$var2 =~ /$regex/)
I guess for an array a foreach can be used, but just for 2 variables only is there a way?
Thanks folks.
In reply to IPv4 regex
by ryan
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|