Hello Regex gurus,
I’m interested in finding “if” statements that contain the negation operator “!” (or potentially several negation operators) anywhere, regardless of blank lines, parentheses, indentation, and so on.
My motivation is to verify, one by one, the scope(s) of the negation(s).
For instance, considere this code snippet:
if ( ID == OK_ID ) OK_ID_procedure (ID);
else if ( ID == fake_ID ) fake_ID_procedure (ID);
else if ( ID == old_ID ) old_ID_procedure (ID);
else if ( ID != lost_ID ) check_ID_procedure (ID);
I would like to select JUST THE LAST “if” statement, which is the only one that contains the negation operator, like so:
The best I have done, so far, is this regex, with a space as its first character to ignore #ifdef and such:
if(?s).+?\(.*?!.*?(;|\{)
It works well but it produces too many “false positives” through which I need to wade.
This regex selects everything from the first “if” in this snippet to the last semicolon:
No big deal, but I thought I would ask the experts for a less greedy regex.
I tried several possible non-greedy options but they produced unexpected/undesirable secondary effects.
Thanks for any hints.
Alfredo