I got the following error while running my perl script with the “negative lookaround” code from Jeffrey Friedl’s book Mastering Regular Expressions 3rd Ed. (page 167). Could anyone help me with it??
Error msg:
Sequence (? incomplete in regex; marked by <– HERE in m/
(
(
(? <– HERE / at /home/wubin28/mastering_regex_cn/p167.pl line 13.
My perl script
#!/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";
}
Your mistake that you use symbol ! for open and close regular expression, and the same time using negative lookahead (?!.). If your change open and close symbol to { and }, or //. Your regexp evaluates fine.