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:  <%-{-{-{-<


In reply to Re: Capturing groups where the ending is optional by AnomalousMonk
in thread Capturing groups where the ending is optional by cwm9

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.