I have a list of allowed fruits and have to ensure that the config file contains all these and only these fruits each in a seperate line as below.The config file has other config lines.Lets say the allowed fruits are apple,banana,cherry then then the valid config file would look like:
fruit apple
some other config line
############
fruit banana
fruit cherry
other config 1
other config n
Any idea of a java regex to check if all the three allowed fruits are listed and only those fruits.For eg: if a fourth fruit is listed in a seperate line then the config file is invalid.
I could achieve with two regexes.One for must contain all and the second to check that it does not contain any other:
Positive - contain all
^(?=.*?^fruit banana)(?=.*?^fruit cherry)(?=.*?^fruit apple).*$
Must not contain anything else
^fruit (?!apple|banana|cherry)
Can anybody come up with just one regex??Thanks.