in reply to Tell or determine whichever Perl regex group fails

They all fail. Eventually, the regex engine will attempt to match at position 4, and not even the first capture will succeed.

  • Comment on Re: Tell or determine whichever Perl regex group fails

Replies are listed 'Best First'.
Re^2: Tell or determine whichever Perl regex group fails
by ikegami (Patriarch) on Jan 04, 2022 at 15:03 UTC

    Anchoring the match would change that.

    'okoK'=~ /^(o|k)(k|i)(O|K)(K)/

    You can alter this pattern to give the the desired information.

    'okoK'=~ / ^ (?: (o|k) (?: (k|i) (?: (O|K) (?: (K) )? )? )? )? /x; my $n = length($&); # -or- use List::Util qw( first ); my $n = first { defined($-[$_]) } 1..4;