I need help scripting a regex that’ll detect when a key phrase ("# of") is present anywhere in a string, then followed by more than 1 set of parentheses.
The rules:
- The string must contain the phrase “# of”, where # is any digit [1 - 9].
- The string must contain more than 1 set of parentheses within the scope of the “# of” phrase.
- The key phrase may be nested, so its scope may be bounded.
Here’s my current regex attempt. https://regex101.com/r/mFUHfP/5
Here are the string examples, plus whether or not they should be matched.
"1 of Apple, Bat, Car, Dog, Ear"
---------------- NO – No sets detected
"3 of (Apple), Bat, Car, Dog, Ear"
------------- NO – Sets < 2.
"2 of (Apple), Bat, (Car), Dog, Ear"
---------- YES – Sets > 1.
"Apple, (Bat), Car, (Dog), Ear"
------------------ NO – Key phrase missing.
"Apple, (2 of Bear, Car, Dog), Ear"
------------ NO – No sets detected in scope.
"Apple, (2 of (Bear), Car, Dog), Ear"
--------- NO – Sets < 2 in scope.
"Apple, (2 of (Bear), Car, (Dog)), Ear"
------ YES – Sets > 1 in scope
"Apple, (Bear), Car, (Dog), Ear"
----------------- NO – Key phrase missing.
"Apple, (2 of Bear, (Car)), (Dog)"
-------------- NO – Sets < 2 in scope.
"Apple or (2 of (Bear or Car), (Dog, Ear))
– YES – Sets < 1 in scope.