RankAmateur
RankAmateur

Reputation: 11

How to RegEx for combinations of forbidden words

I am a retired teacher and have developed a way of automatically marking short answer science questions using regex. I use a more or less standard approach that requires keywords (A, B, C, etc) and rejects forbidden words (X):

/^(?!.*?(X))(?=.*?\b(A))(?=.*?\b(B))(?=.*?\b(C)).{0,150}$/i

I would like to be able reject on the basis of 2 forbidden words (X and Y) being present but where one or the other are accepted.

So A B C; A B C X; A B C Y would all be accepted but A B C X Y would be rejected. Is it possible to do this and possibly more that 2 forbidden words?

By the way, I don't understand much of regex, I just use it!

Upvotes: 1

Views: 133

Answers (1)

Toto
Toto

Reputation: 91395

For the first part of the regex, you could use:

(?!(?=.*X)(?=.*Y))

That means X and Y cannot be present both.

The whole regex:

/^(?!(?=.*X)(?=.*Y))(?=.*\bA)(?=.*\bB)(?=.*\bC).{0,150}$/i

Upvotes: 1

Related Questions