gpt4 book ai didi

匹配 xa?b?c? 的正则表达式但不只是x

转载 作者:行者123 更新时间:2023-12-02 10:58:05 25 4
gpt4 key购买 nike

我正在尝试编写一个匹配 xa?b?c? 的正则表达式但不是x。实际上,“x”、“a”、“b”和“c”不是单个字符,它们是中等复杂的子表达式,所以我试图避免像 x(abc|ab|ac|bc |a|b|c)。有没有一种简单的方法可以在正则表达式中匹配“按顺序至少a、b和c之一”,或者我运气不好?

最佳答案

这是最短的版本:

(a)?(b)?(c)?(?(1)|(?(2)|(?(3)|(*FAIL))))

如果您需要将比赛保留在单独的组中,请写下以下内容:

((a)?(b)?(c)?)(?(2)|(?(3)|(?(4)|(*FAIL))))

但是,如果 abc 包含捕获组,这种方法就不是很可靠。所以改为这样写:

(?<A>a)?(?<B>b)?(?<C>c)?(?(<A>)|(?(<B>)|(?(<C>)|(*FAIL))))

如果您需要一组参加整场比赛,请写下以下内容:

(?<M>(?<A>a)?(?<B>b)?(?<C>c)?(?(<A>)|(?(<B>)|(?(<C>)|(*FAIL)))))

如果像我一样,您更喜欢多字母标识符,并且认为这种事情在不处于 /x 模式的情况下是疯狂的,请写下以下内容:

(?x)
(?<Whole_Match>
(?<Group_A> a) ?
(?<Group_B> b) ?
(?<Group_C> c) ?

(?(<Group_A>) # Succeed
| (?(<Group_B>) # Succeed
| (?(<Group_C>) # Succeed
| (*FAIL)
)
)
)
)

这是完整的测试程序,以证明这些都有效:

#!/usr/bin/perl
use 5.010_000;

my @pats = (
qr/(a)?(b)?(c)?(?(1)|(?(2)|(?(3)|(*FAIL))))/,
qr/((a)?(b)?(c)?)(?(2)|(?(3)|(?(4)|(*FAIL))))/,
qr/(?<A>a)?(?<B>b)?(?<C>c)?(?(<A>)|(?(<B>)|(?(<C>)|(*FAIL))))/,
qr/(?<M>(?<A>a)?(?<B>b)?(?<C>c)?(?(<A>)|(?(<B>)|(?(<C>)|(*FAIL)))))/,
qr{
(?<Whole_Match>

(?<Group_A> a) ?
(?<Group_B> b) ?
(?<Group_C> c) ?

(?(<Group_A>) # Succeed
| (?(<Group_B>) # Succeed
| (?(<Group_C>) # Succeed
| (*FAIL)
)
)
)

)
}x,
);

for my $pat (@pats) {
say "\nTESTING $pat";
$_ = "i can match bad crabcatchers from 34 bc and call a cab";
while (/$pat/g) {
say "$`<$&>$'";
}
}

所有五个版本都会产生以下输出:

i <c>an match bad crabcatchers from 34 bc and call a cab
i c<a>n match bad crabcatchers from 34 bc and call a cab
i can m<a>tch bad crabcatchers from 34 bc and call a cab
i can mat<c>h bad crabcatchers from 34 bc and call a cab
i can match <b>ad crabcatchers from 34 bc and call a cab
i can match b<a>d crabcatchers from 34 bc and call a cab
i can match bad <c>rabcatchers from 34 bc and call a cab
i can match bad cr<abc>atchers from 34 bc and call a cab
i can match bad crabc<a>tchers from 34 bc and call a cab
i can match bad crabcat<c>hers from 34 bc and call a cab
i can match bad crabcatchers from 34 <bc> and call a cab
i can match bad crabcatchers from 34 bc <a>nd call a cab
i can match bad crabcatchers from 34 bc and <c>all a cab
i can match bad crabcatchers from 34 bc and c<a>ll a cab
i can match bad crabcatchers from 34 bc and call <a> cab
i can match bad crabcatchers from 34 bc and call a <c>ab
i can match bad crabcatchers from 34 bc and call a c<ab>

甜甜的,嗯?

编辑:对于开始部分的x,只需在比赛开始时、在比赛开始之前放置您想要的任何x即可。 a 部分的第一个可选捕获组,如下所示:

x(a)?(b)?(c)?(?(1)|(?(2)|(?(3)|(*FAIL))))

或者像这样

(?x)                        # enable non-insane mode

(?<Whole_Match>
x # first match some leader string

# now match a, b, and c, in that order, and each optional
(?<Group_A> a ) ?
(?<Group_B> b ) ?
(?<Group_C> c ) ?

# now make sure we got at least one of a, b, or c
(?(<Group_A>) # SUCCEED!
| (?(<Group_B>) # SUCCEED!
| (?(<Group_C>) # SUCCEED!
| (*FAIL)
)
)
)
)

测试句子是在没有 x 部分的情况下构建的,因此它不起作用,但我想我已经展示了我的意思。请注意,所有 xabc 都可以是任意复杂的模式(是的,甚至是递归的) ),而不仅仅是单个字母,即使它们使用自己的编号捕获组也没关系。

如果你想通过前瞻来解决这个问题,你可以这样做:

(?x)

(?(DEFINE)
(?<Group_A> a)
(?<Group_B> b)
(?<Group_C> c)
)

x

(?= (?&Group_A)
| (?&Group_B)
| (?&Group_C)
)

(?&Group_A) ?
(?&Group_B) ?
(?&Group_C) ?

以下是添加到测试程序中的 @pats 数组中的内容,以表明此方法也有效:

qr{
(?(DEFINE)
(?<Group_A> a)
(?<Group_B> b)
(?<Group_C> c)
)

(?= (?&Group_A)
| (?&Group_B)
| (?&Group_C)
)

(?&Group_A) ?
(?&Group_B) ?
(?&Group_C) ?
}x

您会注意到,即使使用前瞻技术,我仍然设法从不重复任何 abc .

我赢了吗? ☺

关于匹配 xa?b?c? 的正则表达式但不只是x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4168670/

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