Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
The "canonical" way to find all matches in a string is with m//g. For example, to find the starting positions of a substring "abc" in a string you could do:
while ($s =~ /abc/g) { print "found abc at $-[0]\n"; }
Now suppose the pattern we are looking for overlaps with itself -- e.g. is aa. The following:
while ($s =~ /aa/g) { print "found aa at $-[0]\n"; }
does not quite work: if the input string is "baaaad", for example, it would output:
found aa at 1 found aa at 3
It leaves out "found aa at 2", which is entirely expected given how /g is defined (the next match starts after the end of the previous one).

Now, it is straightforward enough to tweak the loop so it finds all the matches, using pos as an lvalue:

while ($s =~ /aa/g) { print "found aa at $-[0]\n"; pos($s) = $-[0] + 1; }
This "tricks" the RE engine to start searching again at the very next character, so all matches will be found, even if they overlap.

This works fine, and solves the particular practical problem I am working on, but it got me to thinking: is it possible to get this bevahior purely declaratively -- i.e. in the RE itself, not by tweaking pos after the match?


Update:Just as I was about to post this, I figured out my own answer, using a lookahead: /(?=aa)./. The lookahead matches without advancing pos, the "." advances pos by 1. Are there other ways to do this? More efficient ways?


In reply to Elegant (i.e. declarative) way to find all possibly overlapping matches by jsegal

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (5)
    As of 2020-01-24 07:44 GMT
    Find Nodes?
      Voting Booth?