Re: basic regex back reference



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)

#!/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 ...

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

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

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

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

.



Relevant Pages