正则表达式包含所有且仅
regex contains all and only
我有一个允许的水果列表,必须确保配置文件包含所有这些并且只有这些水果在单独的行中,因为 below.The 配置文件有其他配置 lines.Lets 说允许的水果是苹果、香蕉、樱桃然后有效的配置文件将如下所示:
fruit apple
some other config line
############
fruit banana
fruit cherry
other config 1
other config n
任何关于 java 正则表达式的想法,以检查是否列出了所有三个允许的水果,并且只列出了那些 fruits.For 例如:如果第四个水果在单独的行中列出,那么配置文件是无效。
我可以用两个 regexes.One 来实现,因为必须包含所有内容,第二个用来检查它是否不包含任何其他内容:
Positive - contain all
^(?=.*?^fruit banana)(?=.*?^fruit cherry)(?=.*?^fruit apple).*$
Must not contain anything else
^fruit (?!apple|banana|cherry)
谁能想出一个正则表达式?谢谢。
这是一个符合您的描述的正则表达式:
^((?!\bfruit\b).)* # nothing with "fruit" before
( # then, either:
# 1. fruit apple, followed by things that are not "fruit", followed by fruit banana, non-fruits, and then fruit cherry, or vice versa
\bfruit\b\ apple((?!\bfruit\b).)*(\bfruit\b\ banana((?!\bfruit\b).)*fruit\ cherry|fruit\ cherry((?!\bfruit\b).)*fruit\ banana)
|
# 2. Same with banana at the beginning
fruit\ banana((?!\bfruit\b).)*(fruit\ apple((?!\bfruit\b).)*fruit\ cherry|fruit\ cherry((?!\bfruit\b).)*fruit\ apple)
|
# 3. And with cherry
fruit\ cherry((?!\bfruit\b).)*(fruit\ apple((?!\bfruit\b).)*fruit\ banana|fruit\ banana((?!\bfruit\b).)*fruit\ apple)
)
((?!\bfruit\b).)*$ # no more fruit
因为我不知道 Java 正则表达式是如何工作的,所以我用 Python 的冗长正则表达式模式做了这个,以便能够将它们写得不那么简洁,但这应该是可在任何有能力的正则表达式引擎中翻译。
加入你的前瞻:
(?ms)^(?!.*?^fruit (?!apple|banana|cherry))(?=.*?^fruit banana)(?=.*?^fruit cherry)(?=.*?^fruit apple).*$
看到 proof 它有效。
开头的 (?!.*?^fruit (?!apple|banana|cherry))
将确保没有以 fruit
开头然后是 space 然后没有 apple
、[=14 的行=] 或 cherry
.
我有一个允许的水果列表,必须确保配置文件包含所有这些并且只有这些水果在单独的行中,因为 below.The 配置文件有其他配置 lines.Lets 说允许的水果是苹果、香蕉、樱桃然后有效的配置文件将如下所示:
fruit apple
some other config line
############
fruit banana
fruit cherry
other config 1
other config n
任何关于 java 正则表达式的想法,以检查是否列出了所有三个允许的水果,并且只列出了那些 fruits.For 例如:如果第四个水果在单独的行中列出,那么配置文件是无效。
我可以用两个 regexes.One 来实现,因为必须包含所有内容,第二个用来检查它是否不包含任何其他内容:
Positive - contain all
^(?=.*?^fruit banana)(?=.*?^fruit cherry)(?=.*?^fruit apple).*$
Must not contain anything else
^fruit (?!apple|banana|cherry)
谁能想出一个正则表达式?谢谢。
这是一个符合您的描述的正则表达式:
^((?!\bfruit\b).)* # nothing with "fruit" before
( # then, either:
# 1. fruit apple, followed by things that are not "fruit", followed by fruit banana, non-fruits, and then fruit cherry, or vice versa
\bfruit\b\ apple((?!\bfruit\b).)*(\bfruit\b\ banana((?!\bfruit\b).)*fruit\ cherry|fruit\ cherry((?!\bfruit\b).)*fruit\ banana)
|
# 2. Same with banana at the beginning
fruit\ banana((?!\bfruit\b).)*(fruit\ apple((?!\bfruit\b).)*fruit\ cherry|fruit\ cherry((?!\bfruit\b).)*fruit\ apple)
|
# 3. And with cherry
fruit\ cherry((?!\bfruit\b).)*(fruit\ apple((?!\bfruit\b).)*fruit\ banana|fruit\ banana((?!\bfruit\b).)*fruit\ apple)
)
((?!\bfruit\b).)*$ # no more fruit
因为我不知道 Java 正则表达式是如何工作的,所以我用 Python 的冗长正则表达式模式做了这个,以便能够将它们写得不那么简洁,但这应该是可在任何有能力的正则表达式引擎中翻译。
加入你的前瞻:
(?ms)^(?!.*?^fruit (?!apple|banana|cherry))(?=.*?^fruit banana)(?=.*?^fruit cherry)(?=.*?^fruit apple).*$
看到 proof 它有效。
开头的 (?!.*?^fruit (?!apple|banana|cherry))
将确保没有以 fruit
开头然后是 space 然后没有 apple
、[=14 的行=] 或 cherry
.