2012-06-21 45 views
2

我从Jeffrey Friedl的书Mastering Regular Expressions 3rd Ed。(page 167)运行我的perl脚本时遇到以下错误,任何人都可以帮助我??序列(?在正则表达式中不完整 - 负查找

错误消息:?

序列(在正则表达式不完整的;标记为< - 这里以m/ ( ( (< - HERE /通过/ home/wubin28/mastering_regex_cn/p167.pl line 13.

我的perl脚本

#!/usr/bin/perl 

use 5.006; 
use strict; 
use warnings; 

my $str = "<B>Billions and <B>Zillions</B> of suns"; 

if ($str =~ m! 
    (
     <B> 
     (
      (?!<B>) ## line 13 
      . 
     )*? 
     </B> 
    ) 
    !x 
    ) { 
    print "\$1: $1\n"; #output: <B>Billions and <B>Zillions</B> 
} else { 
    print "not matched.\n"; 
} 

回答

5
您使用符号

你的错!用于打开和关闭正则表达式,同时使用负向前视(?!。)。如果您的更改打开并关闭符号{和}或//。你的正则表达式评估罚款。

use strict; 

my $str = "<B>Billions and <B>Zillions</B> of suns"; 

if ($str =~ m/(<B>((?!<B>).)*?<\/B>)/x) { 
    print "\$1: $1\n"; #output: <B>Billions and <B>Zillions</B> 
} else { 
    print "not matched.\n"; 
} 
+0

明白了。非常感谢! –

+0

你也可以逃脱! (\!)里面的正则表达式,如果你想使用m!句法 –