1、Anchoring Patterns
\b意味著字符邊界。代碼:
print "what's your name?";
$name=<STDIN>;
chomp($name);
if($name=~ /^randl\b/i){
print "matched\n";
}else{
print "not matched\n";
}
如果輸入randl*, *是字符、數字和下劃線,那么都會打印not matched
而輸入"randl%"、"randl fa"等則matched。
/fred\b/; # matches fred, but not frederick
/\bmo/; # matches moe and mole, but not Elmo
/\bFred\b/; # matches Fred but not Frederick or alFred
/\b\+\b/; # matches "x+y" but not "++" or " + "
/abc\bdef/; # never matches (impossible for a boundary there)
word boundary按字面來看是一種邊界,一個位置。在這個位置兩邊的字符一個與\w匹配,一個與
\W匹配,這是一種情況;還有一種情況就是,一個字符與\w匹配,而另一個字符嘛,要么是
字符串的打頭,要么是結尾。而\b這個anchor恰恰需要在它出現的位置是一個word boundary。
再看來/abc\bdef/,\b兩邊一個是c,一個是d,都與\w匹配,這已不滿足上述的第一種情況,
至于第二種情況,更加不會是字符串的打頭或末尾了。所以\b出現的地方不可能是word boundary。
附Learning Perl中的片段說明:
if ($name =~ /^Randal/) {
## yes, it matches
} else {
## no, it doesn't
}
Note that the regular expression is delimited by slashes. Within the slashes, spaces and other whitespace
are significant, just as they are within strings.
This almost does it, but it doesn't handle selecting randal or rejecting Randall. To accept randal,
we add the ignore-case option, a small i appended after the closing slash. To reject Randall, we add a
word boundary special marker (similar to vi and some versions of grep) in the form of "b in the regular
expression. This ensures that the character following the first l in the regular expression is not another
letter. This changes the regular expression to be /^randal"b/i, which means "randal at the
beginning of the string, no letter or digit following, and OK to be in either case."