I'm trying to come up with with a regex statement that will match and not match the following cases:
CT.test // (1) must match
CT.test (MONT) // (2) must match
CT.test (ABS) // (3) must match
CT.badsf // (4) must not match
CT.test (WOW) // (5) must not match
I've tried CT.test( \(MONT\)| \(ABS\)|^$)
but that only matches cases 2 and 3 and not case 1.
What is a regex statement that will match case 1, 2 and 3 and not match cases 4 and 5?
CodePudding user response:
You can use
^CT\.test(?: \((?:MONT|ABS)\))?$
See the regex demo
See the details here:
^
- start of stringCT\.test
- (note the escaped dot): aCT.test
string(?: \((?:MONT|ABS)\))?
- an optional sequence of\(
- a(
char(?:MONT|ABS)
-MONT
orABS
string\)
- a)
char
$
- end of string.