gpt4 book ai didi

regex - 序列 (? 正则表达式不完整 - 负面环视

转载 作者:行者123 更新时间:2023-12-02 05:40:00 24 4
gpt4 key购买 nike

在使用 Jeffrey Friedl 的书《精通正则表达式》第 3 版中的“negative lookaround”代码运行我的 perl 脚本时,我遇到了以下错误。 (第 167 页)。谁能帮我解决一下??

错误信息:

序列 (? 在正则表达式中不完整;在 m/中用 <-- HERE 标记 ( ( (? <-- 这里/在/home/wubin28/mastering_regex_cn/p167.pl 第 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";
}

最佳答案

你使用 symbol 的错误!用于打开和关闭正则表达式,同时使用否定前瞻 (?!.)。如果您将打开和关闭符号更改为 { 和 },或//。您的正则表达式评估正常。

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";
}

关于regex - 序列 (? 正则表达式不完整 - 负面环视,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11131356/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com