jhu schreef:
> Can anyone tell me using Perl how to find all matching patterns. So if
> I have this one big string of say a html web page which contains
> multiple IP's and my pattern which is \d{1,3}\.\d{1,3}\.\d{1,3}\.
> \d{1,3} then how do I go about setting up my code in some sort of
> while loop to find all instances of matches ?
#!/usr/bin/perl
use strict;
use warnings;
use Regexp::Common;
local $\ = "\n";
(my $big_string = join "", <DATA>) =~ s/\s*\n\s*/ /g;
print $big_string;
print "-" x 40;
my @ips = $big_string =~ m/$RE{net}{IPv4}/g;
print for @ips;
__DATA__
abc 123.45.6.78 xyz
abc 234.45.68.91 xyz
abc 123.456.78.9 xyz
abc 1.2.3.4 xyz
abc 123.45.678 xyz
abc 234.45.68.910 xyz
abc 1.2.3.4 xyz
See also:
http://search.cpan.org/~abigail/Rege.../Common/net.pm
> Also can someone in
> simple terms explain what a back reference is ? Is it simply the
> pattern found ?
A backreference is a sub-pattern. (The word pattern is traditionally
reserved for the whole search pattern.)
With a backreference, you reuse something that is already found by an
earlier capture in the pattern:
m/ ([aeiou]) \1 /x; # two vowels, equal
m/ [aeiou]{2} /x; # two vowels, maybe equal
Now read both perlre and perlretut.
In perlre you'll find:
"Referring back to another part of the match is called a backreference."
In perlretut you'll find:
"Backreferences are simply matching variables that can be used inside a
regexp. This is a really nice feature - what matches
later in a regexp can depend on what matched earlier in the regexp."
--
Affijn, Ruud
"Gewoon is een tijger."