I need to validate a user input that can include parentheses, the word 'or' and the word 'and'.
The rules are simple:
1- Parentheses can be used anywhere but opening parentheses must have closing one (I might validate this part with code). 2- The expression must never end with 'or' or 'and' within a block. For example:
John and Jane -> OK
John and -> NOT OK
(John and) -> NOT OK
John or -> NOT OK
(John or) -> NOT OK
So it always:
[Optional parentheses] WORD [Optional operation and or][Optional parentheses]
I came up with this but it's not enough but it's a start.
\b\w.*(?=or|and).*\w
That works for the content with parentheses
Note: I don't care about the amount of whitespace between words.
The user can combine these expression blocks multiple times.
^\w+(?:\s+(?:or|and)\s+\w+)*$. Instead of\w+, you can have(?:John|Jane).^\(?\w+(?:\s+(?:or|and)\s+\w+)*\)?$.