in reply to Matching regular expressions question
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:
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.while ($text =~ m/.../g) { ... }
It appears you can reset the last match position by either:
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)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Matching regular expressions question
by Ninth Prince (Acolyte) on May 26, 2008 at 23:49 UTC |