- Suppose you’re using
-
the pattern /fred.+barney/ on the string fred and barney went bowling last night.
the .+ is greedy. it will match all of the rest string including night. then give back t, h, and so on until barney to match barney.
For each of the greedy quantifiers, though, there’s also a nongreedy quantifier available.
Instead of the plus (+), we can use the nongreedy quantifier +?, which matches one or
more times (just as the plus does), except that it prefers to match
as few times as pos-
sible, rather than as many as possible.
if we use /fred.+?barney/. it will match the whitespace and then try to match barney, it can not match, so match a and try again to match barney but with no success, it will try until match barney.
- $str = "I thought you said Fred and Velma, not Wilma";
-
-
$str =~ s#<BOLD>(.*?)</BOLD>#$1#g;
-
print $str;
I thought you said Fred and Velma, not Wilma
阅读(382) | 评论(0) | 转发(0) |