in reply to What's like $+ but not gives the ordinal?

Look at @+ instead (documentation in perlvar). It won't quite do exactly what you want, but you'll at least be able to determine (via defined) whether a given paren matched or not).

Another alternative would be to say

/((?:foo)|(?:bar)|(?:baz))/

because then you don't care what matched, you can just refer to $1. If you need to do different things according to what matched, it might just be cleaner to break it up into different regexes, but without more context, it's difficult to say.


--
g r i n d e r

 

Edit: chipmunk 2001-06-27

Replies are listed 'Best First'.
Re: Re: What's like $+ but not gives the ordinal?
by John M. Dlugosz (Monsignor) on Jun 28, 2001 at 01:21 UTC
    scalar @+ always returns the number of paren groups in the RE, regardless of which one matched. Checking defined on the elements is really no different from doing it with the vars directly, but I see the advantage is being able to use a loop with subscripts.

    I can't break it up into different regex's because replacing doesn't have the /G and one-at-a-time feature that just searching does.

    —John