A couple other ways:

c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; print 'Perl version ', $]; ;; my $s = 'abcd'; ;; my @captures = $s =~ m{ (?= (\w\w)) }xmsg; printf qq{'$_' } for @captures; print ''; ;; local our @caps; ()= $s =~ m{ (?: (\w\w) (?{ push @caps, [ $^N, $-[1] ] }) (*F)) }xmsg +; print qq{captured '$_->[0]' at offset $_->[1]} for @caps; " Perl version 5.014004 'ab' 'bc' 'cd' captured 'ab' at offset 0 captured 'bc' at offset 1 captured 'cd' at offset 2
Note that Perl version 5.10+ is needed only for the Special Backtracking Control Verbs  (*F) construct in the second example above. Also, the  (?{ code }) block in the second example must push to a package-global array because this construct doesn't work reliably with pad (my) variables (this was fixed in version 5.18 – I think). (Update: The first example above is very nice and neat if you only need to capture the (overlapping) matched substrings. If more info is needed, e.g., substring offset, one of the other, more messy approaches must be used.)

Update: Another version of the second example can be had that does not depend on any 5.10 regex enhancement (the example below was run under 5.8.9), but it has the quirk that (for a reason I used to know but have since forgotten) all push-es to the array are duplicated! If you can live with that, then:

c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "print 'Perl version ', $]; ;; my $s = 'abcd'; ;; local our @caps; ()= $s =~ m{ (?= (\w\w) (?{ push @caps, [ $^N, $-[1] ] })) }xmsg; dd \@caps; ;; my $skip; print qq{captured '$_->[0]' at offset $_->[1]} for grep $skip = !$skip, @caps; " Perl version 5.008009 [["ab", 0], ["ab", 0], ["bc", 1], ["bc", 1], ["cd", 2], ["cd", 2]] captured 'ab' at offset 0 captured 'bc' at offset 1 captured 'cd' at offset 2

Update 2: Actually, in the regex of the
    ()= $s =~ m{ (?: (\w\w) (?{ push @caps, [ $^N, $-[1] ] }) (*F)) }xmsg;
statement of the second example above, the outermost  (?: ... ) non-capturing grouping is completely useless. The
     m{ (\w\w) (?{ push @caps, [ $^N, $-[1] ] }) (*F) }xmsg
regex works just as well.


In reply to Re: Controlling matching position in regexes to match all proper substrings by AnomalousMonk
in thread Controlling matching position in regexes to match all proper substrings by pat_mc

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.