Hello, @indrk-toyou, @peterjones and All,
Ah ! Peter, I needed some time to figure out the correct regex, but it should, indeed, match all occurrences of the word foo, which are, both, outside multi-lines comments /*.....*/ and before any // start of a line comments ;-))
Note that finding a regex that matches each kind of C comments was rather easy ! However taking these two features in account, in a same regex, was really a tricky work !!
So, if options Regular expression and Wrap around are selected, in the Find/Replace/Mark dialog, a possible solution is :
(?-i)((?s)/\*.*?\*/((?!/\*).)*?|(?-s)(\G|^)((?!//|/\*).)*?)\Kfoo , if you want to match the word foo, with its exact case
(?i)((?s)/\*.*?\*/((?!/\*).)*?|(?-s)(\G|^)((?!//|/\*).)*?)\Kfoo , if you want to match the word foo, whatever its case
So, assuming the text below ( Please, no coherent C code, Just to verify that the regex does work as expected ! )
The regex finds /marks 21 occurrences of foo !
"foo" // First match
void foo(void) foo { // Match the word foo, before //, but NOT these foo, located after FIRST //
foo
int bar = foo(); foo // match the TWO strings "foo"
foo
foo foo /* bar = recurse() + foo(); // foo INSIDE a multi-lines comment, so should be IGNORED
i = foo(); // Word "foo" still IGNORED !
foo */ foo = foo * 7 // The FIRST "foo" is IGNORED, only !
/* foo A SECOND multi-lines comment, containing some foo strings
foo
foo*/
/* foo And a THIRD multi-lines comment !
foo*/
return( foo()); // Match "foo" BEFORE the // symbol for comment LINE
foo()
foo/* foo */foo // One-line comment block with foo, surrounded with two words "foo" OUTSIDE comment
foo/**/foo // "foo" OUTSIDE One-line comment block
foo /* foo Two consecutive TWO-lines comment blocks, containing foo, split on THREE lines
*/ foo /*
foo */ foo
}
s = "foo" // LAST item (foo)
Notes :
Of course, you may replace the word foo by any string or, even, a complete regex !
This regex does not handle some oddities as, for instance, */......./* or nested multi-lines comments /*..... /*.....*/....... */
BEWARE : If you need to delete or replace all occurrences of the string foo, outside comments, with something else, you must use the Replace All button, exclusively and not the Replace button ( step-by-step replacement )
Allow me to not explain this regex, right now ! I need to think about it, again. May be an easier solution will comes to my mind ;-))
Probably, I just missed a very easy way ! ( In France, we have an expression : “like the tree which hides the forest” ! ) So, I’m waiting your solutions !
Best regards,
guy038