I would like to get the result:my $regex = qr/(\d\d):(\d\d)(?::(\d\d))?/; # 01234567890123456 my $string = "11:30 or 11:29:53";
$res = { 0 => ['11', '30', undef], 19 => ['11', '29', '53'] }
If I evaluate the the the regex in scalar context, I can get the position of each match, like this:
But the problem is that I read the regex from a file, so I have no idea how many capturing brackets it contains (and I think it is rather hard to find that out, isn't it?). Therefore I don't how many $n to put in the result array.$res->{pos ($string)} = [$1, $2, $3] while $string = /$regex/g;
On the other hand, I can evaulate the regex in list context, as in:
but then I get neither the positions nor the information how often the string matched (I don't know how many capturing brackets the regex contains!). Is there any way to get the contents of the brackents in an array, but only for one match, and to iterate over all matches in a while loop, as m//g does in scalar context?@res = $string = /$regex/g;
Thanks for any advice,
pike
In reply to Multiple matches of a regex by pike
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |