您的位置:首页 > 其它

Perl 单词边界

2014-12-04 19:14 169 查看
单词边界:

\b匹配单词边界,就是位于单词(\w)和非单词字符(\W)之间的零宽度的地方。

就是单词前后必须跟非单词字符

[oracle@jhoa big]$ cat 11.pl
#$var = "The Great Fred";
$var = "Fred erick the Great";
if ($var =~/\bFred\b/){print "$var\n"};

[oracle@jhoa big]$ perl 11.pl
Fred erick the Great

[oracle@jhoa big]$ cat 11.pl
#$var = "The Great Fred";
$var = "aFred erick the Great";
if ($var =~/\bFred\b/){print "$var\n"};
[oracle@jhoa big]$ perl 11.pl
[oracle@jhoa big]$

Fred前面出现单词字符 ,匹配不上

[oracle@jhoa big]$ cat 11.pl
#$var = "The Great Fred";
$var = "Freda erick the Great";
if ($var =~/\bFred\b/){print "$var\n"};
[oracle@jhoa big]$ perl 11.pl
[oracle@jhoa big]$

Fred后面出现单词字符匹配不上

[oracle@jhoa big]$ cat 11.pl
#$var = "The Great Fred";
$var = "!Fred erick the Great";
if ($var =~/\bFred\b/){print "$var\n"};
[oracle@jhoa big]$ perl 11.pl
!Fred erick the Great
[oracle@jhoa big]$

单词前面出现非单词字符,可以匹配

[oracle@jhoa big]$ cat 11.pl
#$var = "The Great Fred";
$var = "Fred! erick the Great";
if ($var =~/\bFred\b/){print "$var\n"};
[oracle@jhoa big]$ perl 11.pl
Fred! erick the Great
[oracle@jhoa big]$

单词后面出现非单词字符,可以匹配
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: