For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > May 2006 > basic regex back reference









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author basic regex back reference
gcr

2006-05-31, 4:01 am

Newbie alert. Thanks for any help.

Can't get back references - I want to pull the bracketed numbers (and
put them in an array)

#!/usr/bin/perl

use warnings;
use strict;

my $output = "5[64]790[908]90567[5678]101";
$output =~ m/(\[+[*\d]+\])/g;
print "$1\n";

prints [64]

was hoping to find [908] in $2 and [5678] in $3 ...
Aukjan van Belkum

2006-05-31, 4:01 am

gcr wrote:
> Newbie alert. Thanks for any help.
>
> Can't get back references - I want to pull the bracketed numbers (and
> put them in an array)


Change the following line
> $output =~ m/(\[+[*\d]+\])/g;


with:

my @array = ($output =~ m/(\[+[*\d]+\])/g);

Now you will find your elements in '@array'

Aukjan.
Josef Moellers

2006-05-31, 4:01 am

gcr wrote:
> Newbie alert. Thanks for any help.
>=20
> Can't get back references - I want to pull the bracketed numbers (and=20
> put them in an array)
>=20
> #!/usr/bin/perl
>=20
> use warnings;
> use strict;
>=20
> my $output =3D "5[64]790[908]90567[5678]101";
> $output =3D~ m/(\[+[*\d]+\])/g;
> print "$1\n";
>=20
> prints [64]=20
>=20
> was hoping to find [908] in $2 and [5678] in $3 ...


You can use a while loop to pull out subsequent matches (dunno if=20
there's a proper word for that):

use warnings;
use strict;
my @array;
my $output =3D "5[64]790[908]90567[5678]101";
while ($output =3D~ m/(\[+[*\d]+\])/g) {
push @array, $1;
}
print join(",", @array), "\n";

This will print
[64],[908],[5678]

--=20
Josef M=F6llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

gcr

2006-05-31, 4:01 am

In article <252e$447d55a6$c2abfc64$22218@news1.tudelft.nl>,
Aukjan van Belkum <aukjan@gmail.com> wrote:

> my @array = ($output =~ m/(\[+[*\d]+\])/g);


works great! thanks!
(I still don't understand but the pain is gone)

The original only matches once(?)
and/or I'm not understanding the $1 $2 functionality(?)

Thanks again!
Dr.Ruud

2006-05-31, 8:05 am

gcr schreef:

> /(\[+[*\d]+\])/


The "\[+" means: one or more literal "["s.

The "[*\d]" is a character class off a literal asterisk and a digit, so
it tries to match either an asterisk or a digit. The "+" behoind it
means "one or more of the previous" again.

ITYM: /(\[\d+\])/
which means: a literal "[", followed by one or more digits, followed by
a literal "]".

--
Affijn, Ruud

"Gewoon is een tijger."


John W. Krahn

2006-05-31, 8:05 am

gcr wrote:
> In article <252e$447d55a6$c2abfc64$22218@news1.tudelft.nl>,
> Aukjan van Belkum <aukjan@gmail.com> wrote:
>
>
> works great! thanks!
> (I still don't understand but the pain is gone)
>
> The original only matches once(?)
> and/or I'm not understanding the $1 $2 functionality(?)


$1 contains the match in the first set of parentheses and $2 contains the
match in the second set of parentheses. You only have one set of parentheses
in that pattern.


John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com