I'm trying to catch, using a single regular expression, a specific term, i.e. "aa" in any of the following lines:
But NOT catch it any of the following lines:
The general rules are:
I always know the exact term I'm looking for.
Each line is evaluated separately.
Only catch terms that are in parentheses, alone or in any position in a comma separated list.
The list can be of any length and with or without spaces between the items.
Catch only the terms without the commas or spaces.
I'm using re in python 3
Any ideas?
If i want to catch a single item in parentheses, the following expression works: (?<=()(?:[ ])aaa(?:[ ])(?=))
If I don't care about the parentheses, the following expression works (in most cases): (?<=[( ,])aa(?=[) ,])
But I can't find a way to combine between the two expressions
You can put all the allowed characters in your lookarounds:
(?:(?<=[(,])|(?<=\( ))aa(?=[),]| \))
See example