in reply to Re^4: Efficient regex matching with qr//; Can I do better?
in thread Efficient regex matching with qr//; Can I do better?

I have probably misunderstood you somehow because this solution doesn't need named captures as far as I can tell.

It only needs named captures if you want to know which one of the regexes matched.

But then I'm unable to match at the same position.

Yes, that's true. If you really need it, and only have a relatively small number of matches, you can do something along these lines:

my $re = assemble_regex(\%hash); my $old_pos = pos; while (m/\G$re/){ $pos = $old_pos; # reset match position # ... extract name of matched regex in $matched_re here ... delete $hash{$matched_re}; # re-generate regex, without the one that previously matched: $re = assemble_regex(\%hash); }

Note that it'll be rather expansive to build the regex many times, so only do this if you have a relatively low number of matches.

Replies are listed 'Best First'.
Re^6: Efficient regex matching with qr//; Can I do better?
by kruppy (Initiate) on Jul 14, 2008 at 18:07 UTC
    "It only needs named captures if you want to know which one of the regexes matched."

    Sorry if I'm slow, but can't I just as well find that out with the $+ operator as in my previous post? Or is that less efficient than using these named captures?
      No, I'm being slow. In the general case it doesn't work (because you can't infer from the matched text what the pattern looks like), but in your case you can. I forgot how your string look like when I wrote my reply. Sorry for the confusion.
        No problem at all. Thanks a lot for your answers.