我正在学习具有环视(lookbehind 和 lookahead)功能的正则表达式,但我无法在 lookbehind 中使用 .*
或 .+
量词(但是在 lookahead 中可以使用)。
我正在尝试修复的正则表达式如下:
(?<!yellow.*)blue(?=.*brown)
这个想法是匹配不含 yellow
,但只有在蓝色之后存在 brown
的行。以下是一些示例:
yellow blue brown // shouldn't match
f blue brown // should match
sdff blue brown // should match
asdf f blue c // shouldn't match
yellow blue fblue b f brown // shouldn't match
以下是我的测试:
http://regex101.com/r/fY4kI9/5
我收到的错误信息是:
. * Lookbehinds需要是零宽度,因此不允许使用量词
您知道如何解决这个问题吗?
{0, 100}
或类似方法来欺骗其中的一些。 - Boris the Spider