Regex惰性模式不像预期的那样工作。

时间:2022-12-13 21:44:36

Given the following string:

给定下面的字符串:

FFSMQWUNUPZRJMTHACFELGHDZEJWFDWVPYOZEVEJKQWHQAHOCIYWGVLPSHFESCGEUCJGYLGDWPIWIDWZZXRUFXERABQJOXZALQOCSAYBRHXQQGUDADYSORTYZQPWGMBLNAQOFODSNXSZFURUNPMZGHTA

I'm trying to match every substring that contains CABDA with the following regex:

我试着匹配每个包含CABDA的子字符串,其中包含以下regex:

C.*?A.*?B.*?D.*?A

The only thing I find then is

我唯一发现的就是。

CFELGHDZEJWFDWVPYOZEVEJKQWHQAHOCIYWGVLPSHFESCGEUCJGYLGDWPIWIDWZZXRUFXERABQJOXZALQOCSAYBRHXQQGUDA

Which in itself is not wrong - but I should be finding CSAYBRHXQQGUDA

这本身并没有错——但我应该找到CSAYBRHXQQGUDA ?

What am I missing?

我缺少什么?

You can test it here if you'd like

你可以在这里测试一下。

Any help is appreciated.

任何帮助都是感激。

3 个解决方案

#1


0  

(?=(C.*?A.*?B.*?D.*?A))

Put your expression inside lookahead to get all matches.See demo

把你的表情放在前面,以得到所有的匹配。看到演示

https://regex101.com/r/fM9lY3/46

https://regex101.com/r/fM9lY3/46

If you want to find only the shortest you can use

如果你想找到最短的,你可以使用。

C(?:(?!C|A|B|D).)*A(?:(?!C|A|B|D).)*B(?:(?!C|A|B|D).)*D(?:(?!C|A|B|D).)*A

#2


2  

A lazy quantifier doesn't mean that it would try to match the smallest substring possible. It just means that it would try to match as little characters as it can and backtrack towards more characters, as opposed to match as many characters as it can and backtrack towards less.

一个懒惰的量词并不意味着它会试图匹配最小的子字符串。它只是意味着它会尽量匹配尽可能少的字符,并回溯到更多的字符,而不是匹配尽可能多的字符,并回溯到更少的字符。

Finding the position remains the same - the first one from left to right. For example:

找到这个位置仍然是一样的——从左到右的第一个位置。例如:

x+?y

when matched against:

当匹配:

xxxy

will still match xxxy and not just xy since it was able to start from the first x and backtrack towards more xes.

将仍然匹配xxxy,而不仅仅是xy,因为它可以从第一个x开始,并回溯到更多的x。

#3


1  

You can use this negation class based regex:

您可以使用基于regex的这个否定类:

/C[^C]*?A[^A]*?B[^B]*?D[^D]*?A/

RegEx Demo

RegEx演示

This finds CSAYBRHXQQGUDA in your given input.

这在给定的输入中找到了CSAYBRHXQQGUDA。

#1


0  

(?=(C.*?A.*?B.*?D.*?A))

Put your expression inside lookahead to get all matches.See demo

把你的表情放在前面,以得到所有的匹配。看到演示

https://regex101.com/r/fM9lY3/46

https://regex101.com/r/fM9lY3/46

If you want to find only the shortest you can use

如果你想找到最短的,你可以使用。

C(?:(?!C|A|B|D).)*A(?:(?!C|A|B|D).)*B(?:(?!C|A|B|D).)*D(?:(?!C|A|B|D).)*A

#2


2  

A lazy quantifier doesn't mean that it would try to match the smallest substring possible. It just means that it would try to match as little characters as it can and backtrack towards more characters, as opposed to match as many characters as it can and backtrack towards less.

一个懒惰的量词并不意味着它会试图匹配最小的子字符串。它只是意味着它会尽量匹配尽可能少的字符,并回溯到更多的字符,而不是匹配尽可能多的字符,并回溯到更少的字符。

Finding the position remains the same - the first one from left to right. For example:

找到这个位置仍然是一样的——从左到右的第一个位置。例如:

x+?y

when matched against:

当匹配:

xxxy

will still match xxxy and not just xy since it was able to start from the first x and backtrack towards more xes.

将仍然匹配xxxy,而不仅仅是xy,因为它可以从第一个x开始,并回溯到更多的x。

#3


1  

You can use this negation class based regex:

您可以使用基于regex的这个否定类:

/C[^C]*?A[^A]*?B[^B]*?D[^D]*?A/

RegEx Demo

RegEx演示

This finds CSAYBRHXQQGUDA in your given input.

这在给定的输入中找到了CSAYBRHXQQGUDA。