What's going on? Do I need to "reset" the search back to the beginning of the $content string?
Yes, you do. The conventional way to use the
/g modifier is with a
while loop:
while ($text =~ m/.../g) {
...
}
When the
while loop terminates the match position associated with
$text is at the end, so the next time you match it with a
/g modifier the search will begin from the start of the start of the string.
It appears you can reset the last match position by either:
- modifying the variable (even $text .= '' works)
- explicitly setting pos(...), i.e. pos($text) = 0
Here is some test code:
my $text = "aaa";
while ($text =~ m/a/g) { last } # only executes once
pos($text) = 0;
# $text .= ''; # another way to do it
print "matched\n" if ($text =~ m/aaa/g)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.