I’ve got some data that I’m parsing in Perl, and will be adding more and more differently formatted data in the near future. What I would like to do is write an easy-to-use function, that I could pass a string and a regex to, and it would return anything in parentheses. It would work something like this (pseudocode):
sub parse {
$data = shift;
$regex = shift;
$data =~ eval ("m/$regex/")
foreach $x ($1...$n)
{
push (@ra, $x);
}
return \@ra;
}
Then, I could call it like this:
@subs = parse ($data, '^"([0-9]+)",([^:]*):(\W+):([A-Z]{3}[0-9]{5}),ID=([0-9]+)');
As you can see, there’s a couple of issues with this code. I don’t know if the eval would work, the ‘foreach’ definitely wouldn’t work, and without knowing how many parentheses there are, I don’t know how many times to loop.
This is too complicated for split, so if there’s another function or possibility that I’m overlooking, let me know.
Thanks for your help!
In list context, a regular expression will return a list of all the parenthesized matches.
So all you have to do is:
And assuming that it matched,
@matcheswill be an array of the two capturing groups.So using your regex:
Also, when you have long regexes, Perl has the
xmodifier, which goes after the closing regex delimiter. Thexmodifier allows you to put white-space and newlines inside the regex for increased readability.If you are worried about the capturing groups that might be zero length, you can pass the matches through
@subs = grep {length} @substo filter them out.