in reply to Capturing groups where the ending is optional

Here, in the Test::More format that may, in future, avoid a great deal of confusion, is a possible solution based on ikegami's solution. Note that the  => (fat comma) in an expression like
    [ 'leftrightabc' => 'left', 'rightabc' ],
is just a notational indulgence that is intended to be read as "produces" or "yields" and that is syntactically just a plain old  , (comma) (update: because its LHS is fully quoted).

c:\@Work\Perl\monks>perl -wMstrict -le "use Test::More 'no_plan'; use Test::NoWarnings; ;; use constant TEST_1 => ( [ 'left' => 'left', '' ], [ 'right' => '', 'right' ], [ 'rightabc' => '', 'rightabc' ], [ 'leftright' => 'left', 'right' ], [ 'leftrightabc' => 'left', 'rightabc' ], ); ;; my $specific_string = qr{ right }xms; ;; my $right_side = qr{ $specific_string .* \z }xms; my $left_side = qr{ (?! $specific_string) . }xms; ;; VECTOR: for my $ar_vector (TEST_1) { my ($string, @expected) = @$ar_vector; ;; is_deeply [ $string =~ m{ \A ($left_side*) ($right_side?) }xms ], \@expected, qq{'$string' -> '$expected[0]' '$expected[1]'}; } ;; done_testing; " ok 1 - 'left' -> 'left' '' ok 2 - 'right' -> '' 'right' ok 3 - 'rightabc' -> '' 'rightabc' ok 4 - 'leftright' -> 'left' 'right' ok 5 - 'leftrightabc' -> 'left' 'rightabc' 1..5 ok 6 - no warnings 1..6

Update: Note also that  $specific_string could be any  qr// pattern.


Give a man a fish:  <%-{-{-{-<