Perl中的匹配边界和选择替代项
Perl中的匹配边界
\b 在Perl中任何字边界一致,如由\W类和\W类之间的差定义的。由于\w包含单词的字符,而\W包含相反的字符,因此通常表示单词的终止。该 \乙 断言这不是一个单词边界的任意位置相匹配。例如-
/\bcat\b/ # Matches 'the cat sat' but not 'cat on the mat' /\Bcat\B/ # Matches 'verification' but not 'the cat on the mat' /\bcat\B/ # Matches 'catatonic' but not 'polecat' /\Bcat\b/ # Matches 'polecat' but not 'catatonic'
在Perl中选择替代品
|字符就像Perl中的标准或按位或。它在正则表达式或组中指定备用匹配项。例如,要在表达式中匹配“cat”或“dog”,您可以使用以下代码-
if ($string =~ /cat|dog/)
您可以将表达式的各个元素组合在一起,以支持复杂的匹配。搜索两个人的名字可以通过两个单独的测试来完成,如下所示:
if (($string =~ /Martin Brown/) || ($string =~ /Sharon Brown/)) This could be written as follows if ($string =~ /(Martin|Sharon) Brown/)