in reply to Grab "as many occurrences as there are" in a long string

Actually, depending on what something is, it's either easy, or somewhat harder to do with a regexp. For instance, if something is /.*/, than the string
"zero FOO one two BAR three FOO four five BAR six"
contains three occurrences of "FOO something BAR". You can still get all matches with a regexp:
$_ = "zero FOO one two BAR three FOO four five BAR six"; /(FOO.*BAR)(??{ say $1 })(*FAIL)/; __END__ FOO one two BAR three FOO four five BAR FOO one two BAR FOO four five BAR

Replies are listed 'Best First'.
Re^2: Grab "as many occurrences as there are" in a long string
by locked_user sundialsvc4 (Abbot) on Sep 24, 2010 at 13:30 UTC

    Now... that is “Regexp Voodoo!”  :-D

    “Surely, I am standing in the presence of a Greater Perl Being,™” he said humbly.

    I was thinking about the “greedy” modifier, and how one would need to use this to avoid simply matching the entire string in a case like the one you have cited.   I am quite befuddled by the incantation you have so wisely uttered here.   Where can I perldoc to read more about such Higher Magick, that I may one day Impress My Friends, too?

      I was thinking about the “greedy” modifier, and how one would need to use this to avoid simply matching the entire string in a case like the one you have cited.
      Ah, you mean as in "something equals /.*?/"? Then here's a question for you. How many times does "FOO.*?BAR" occur in "zero FOO one FOO two BAR three BAR four"?
      Where can I perldoc to read more about such Higher Magick, that I may one day Impress My Friends, too?
      Except for say, all will be revealed by reading man perlre. (Oh, and if you replace "(*FAIL)" with "(?!)", you could have done the trick ever since 5.005 - it's not something from this century).

      Note also that

      /(FOO.*?BAR)(??{ say $1 })(*FAIL)/;
      and
      /(FOO.*BAR)(??{ say $1 })(*FAIL)/;
      print exactly the same matches, although in a possible different order.