We all know (or should), that the g option in regular expressions doesn't actually give you "all occurrences" of a substring in a string. For example:

perl -wle '$_="ababa";(@matches) = /aba/g; print @matches;'

prints "aba" only once, even though there are TWO occurences of "aba" in the original string.

I'm faced with a similar situation. I'm trying to scan a string of DNA for ALL occurrences of /..g.{18}c/. For any given (random) sequence, there is about a 6% chance of matching the above substring. However, when used like the following:

my @string_var = $DNA_sequence =~ /..g.{18}c/ig;

then we only get 1/2 of the predicted amount. This is entirely due to the fact that given the substring length/composition, there is a very good chance that two matching patterns overlap (like in the "ababa" example).

So here's the question: How do I get around this so that I can match the missing substrings? Can it be done with a regexp, or substr, or is there a module that can help?

In reply to regexp g option: finds all occurrences? ha!! by cyphedude

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.