正则表达式,使用 Perl 将句子中的单词与单词边界匹配



是将以下句子中的单词与单词边界匹配的一种方式,并且应该将单词与单词右侧或左侧的破折号,braket,逗号,句号等匹配。

例如:

 $str = "The quick brown fox (jump) over the lazy dog yes jumped, fox is quick jump, and jump-up and jump.";

如何使用 Perl 正则表达式匹配示例句子中单词"jump"的 4 次出现?

注意:我不想匹配"跳跃"一词。

my @words = $str =~ m{bjumpb}g;
print "@wordsn";

单词边界 ("\b") 是两个字符之间的一个点,具有 "\w"在它的一侧,"\W"在它的另一侧(在任何一个 顺序),计算开头和结尾的虚构字符 匹配"\W"的字符串。

-- Perldoc Perlre> Assertions

foreach($str=~/b(jump)b/g){
    print "$1n";
}
print "$_n" for $str =~ /bjumpb/g;
my @arr = $string =~ m{bjumpb}g;
print "@arrn";

最新更新