Re: basic regex back reference
- From: Josef Moellers <josef.moellers@xxxxxxxxxxxxxxxxxxx>
- Date: Wed, 31 May 2006 10:42:03 +0200
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
.
- References:
- basic regex back reference
- From: gcr
- basic regex back reference
- Prev by Date: basic regex back reference
- Next by Date: Re: basic regex back reference
- Previous by thread: Re: basic regex back reference
- Next by thread: Re: basic regex back reference
- Index(es):
Relevant Pages
|
|