I’m looking to create a VBA regular expression that will find the existence of two particular strings inside a set of parentheses.
For example, in this expression:
(aaa, bbb, ccc, ddd, xxx aaa)
it should somehow tell me that it found both “aaa” AND “xxx aaa” in the expression. I.e, since there is a match on “aaa” without the “xxxx ” in front, and there is also a match on “xxx aaa” later on in the expression, it should return true. Since these two sequences can appear in either order, the reverse should also be true.
So I’m thinking the expression/s would be something like this:
“(xxx aaa”[^x][^x][^x][^x]aaa)”
to find the words in one order and
“(aaa”[^x][^x][^x][^x]xxx aaa)”
for the words in another order.
Does this make sense? Or is there a better approach?
I know this is changing the spec, but there is one important addendum – there cannot be any interceding parentheses between the terms.
So for example, this should’t match:
(aaa, bbb, ccc, ddd, (eee, xxx aaa))
In other words I’m trying to look in between a matching set of parentheses only.
Zero-width look-ahead asserttions are your friend.
This pattern reads as:
term1occurs before the closing parenterm2occurs before the closing parenSince I’m using look-ahead (
(?=...)), the regex engine never actually moves forward on the string, so I can chain as many look-ahead assertions and have them all checked. A side-effect is that the order in whichterm1andterm2occur in the string doesn’t matter.I tested it on the console (“Immediate Window”):
Notes:
Truebecause—technically—bothaaaandxxx aaaare inside the same set of parens.FWIW, here’s a minimal nesting-aware function that works for the second test case above:
Console: