I have a very large string, $text. The string will have one or more start delimiters, foo, and an equal number of end delimters, bar. between the two will be an arbitrary amount of text.
example:
I devised the following :foo this is one example bar this is a line I don't care about foo here's another keeper! bar foo yet another bar and a line to reject
that worked ok for the first one, but only captured the first instance. "Ah", I thought, "I'm not being greedy enough." Thus arosewhile ($text =~/foo(*.?)bar/) { manipulate ($1) }
but the same thing happened -- the while loop would only process once, and then move on. I spent some Quality Time with the Bookshelf, perlre and perlop, and came up with :while ($text =~/foo(*.?)bar/g) { manipulate ($1) }
where \G matches from the last greedy regexp. That never seems to match, even once, so I thought some more. I figured "maybe I need to initialize the regexp somehow :while ($text =~/\Gfoo(*.?)bar/g) { manipulate ($1) }
And that gave the same results, matched the first (non-loop) regexp, and ignored the regexp in the while loop. At this point, I'm pretty stumped. I've trawled through the regexp questions in the monastery, but most are labelled "regexp questions for the experts" or "quick regexp question" or, my favorite "available string 2463".($text =~/foo(*.?)bar/g) manipulate ($1); while ($text =~/\Gfoo(*.?)bar/g) { manipulate ($1) }
In reply to Delving the regexp underdark -- how to understand \G's behavior, or how to loop through a regular expression by boo_radley
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |