in reply to Re^2: getting a regex to return an array of matches?
in thread getting a regex to return an array of matches?
This will:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use diagnostics; undef $/; my $text = <DATA>; my @matches; if (@matches = $text =~ /alpha\|([^|]+)\|/smg) { foreach my $match (@matches) { print "match: $match\n"; } } __DATA__ alpha|beta|alpha|theta|alpha|gamma|alpha|episilon
Output:
match: beta match: theta match: gamma
Or you could dispense with loading data into the array at all (unless you need it for later):
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use diagnostics; undef $/; my $text = <DATA>; while ($text =~ /alpha\|([^|]+)\|/smg) { print "match: $1\n"; } __DATA__ alpha|beta|alpha|theta|alpha|gamma|alpha|episilon
|
|---|