--Edit-- The current answers have some useful ideas but I want something more complete that I can 100% understand and reuse; that\'s why I set a bounty. Als
Your requirement that it's not inside parens in impossible to satify for all cases.
Namely, if you can somehow find a (
to the left and )
to the right, it doesn't always mean you are inside parens. Eg.
(....) + 55555 + (.....)
- not inside parens yet there are (
and )
to left and right
Now you might think yourself clever and look for (
to the left only if you don't encounter )
before and vice versa to the right. This won't work for this case:
((.....) + 55555 + (.....))
- inside parens even though there are closing )
and (
to left and to right.
It is impossible to find out if you are inside parens using regex, as regex can't count how many parens have been opened and how many closed.
Consider this easier task: using regex, find out if all (possibly nested) parens in a string are closed, that is for every (
you need to find )
. You will find out that it's impossible to solve and if you can't solve that with regex then you can't figure out if a word is inside parens for all cases, since you can't figure out at a some position in string if all preceeding (
have a corresponding )
.