Hi everyone!
I was writing a script to filter ip addresses. And used two (simple and incomplete) regexes:
my $ip4 = "([0-9]{1,3}\.){3}[0-9]{1,3}";
my $ip6 = "([a-f0-9]{4}\:)+?";
my $prefix = "/\d\d";
They aren't (and don't need to be) fully correct. I understand that these regexes match an invalid ips like 999.888.777.666, but the ips I have on the list are ok.
My question resides here:
my $pattern = "^($ip4|$ip6)($prefix)?";
It is intented to catch and ip address, followed or not by a prefix. But the resulting match does not catch the prefix:
open (FILE, "<$some_file") or die "Error: $!\n";
while (<FILE>) {
next if !~ m[$pattern];
print "Hey, I found \$1: $1 \$2: $2 \$3: $3\n";
}
close FILE;
I don't understand how could one use the groups inside two or three levels into parenthesis.
You see, the $pattern variable looks this ugly:
^(([0-9]{1,3}\.){3}[0-9]{1,3}|([a-f0-9]{4}\:)+?)(/\d\d)?
Considering this situation, how the matches are set into $1, $2... variables?
Is there a way to set the ip address into $1 and the $prefix into $2 ?
If possible, I'd like to use $ip4 instead of:
"[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}"
### Update ###
There was a little problem about my english, sorry about that. Where I typed 'mask' I wanted to mean 'prefix' (the '/\d\d' part of an ip address). Maybe this is what Kenosis asked (if I understand the question).
-
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.