The typical expression to illustrate this is   /(.)*/s That will match last char in the string, if any. If you step back from the screen and look at the pattern again, you might think this makes sense. Looking at the capturing part (.) I think you want $1 to be one char long.

Expanding the issue a bit, would you want   'abcd' =~ /(?:(.)(.))*/s or   'abcd' =~ /(?:(.){2})*/s to set
$1 eq 'a' $2 eq 'b' $3 eq 'c' $4 eq 'd'
?

What potentially could get really messy would be if you have another group and the end:   'abcdx' =~ /(?:(.)(.))*(.)/s How would you easily know what the last match matched? (Ignoring Re: Multiple matches of a regex.) Sure, you can use $+, or even $^N in recent perls. But what if it's the second last match?

This also leads the question to how you'd do backreferences, if you at regex compile-time can't decide which variable that will hold the submatch.

But this being Perl you of course can do what you want. Here's a little demonstration where I want to match subsequent words with nothing but spaces in between:
$_ = 'foo bar baz burk | gah'; my @words; /(?:(\w+)\s+(?{push @words => $1}))*/; # Not backtracking safe! See +below. # Submatches are in @words now.
If we look back at the issue of backreferencing you can use (??{}) to create dynamic backreferences. This pattern below requires the last two words to be identical (but it doesn't include the last word in @words; compare to /(.)\1/).
my @words; 'foo bar baz baz burk | gah' =~ / (?{ local @_words }) (?: (\w+) \s+ (?{ local @_words = (@_words, $1) }) )+ (??{ quotemeta $_words[-1] }) (?{ @words = @_words }) /x;
This version is also backtracking safe. The one above wasn't, but it didn't need to. As you see it's a bit of extra work to make it backtracking safe so I kept it simple in the one that didn't need it.

Hope I've helped,
ihb

In reply to Re: Capturing brackets within a repeat group [plus dynamic backreferences] by ihb
in thread Capturing brackets within a repeat group by BrowserUk

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.