Visual Studio Regular Expressions and C Preprocessor Macros

Ever run across a need to replace a series of macros (or function calls for that matter) with something almost identical except for a miniscule change?

For example, suppose you've got a debugging macro that takes a format string followed by format variables. Suppose also that this macro doesn't append newlines to the format string. After a while it becomes nearly impossible to distinguish one log message from another. I ask you to suspend disbelief with regard to the obvious question "Why didn't the programmer put a newline at the end of each log message?"

Visual Studio supports regular expressions with a few syntactically small but semantically significant differences from PERL5 regular expressions. PERL5 uses parenthesis both to group/alternate AND capture subexpressions. Visual Studio supports these concepts with distinct operators: parenthesis for grouping/alternation and braces {} for subexpression capture. It refers to subexpressions as tagged expressions.

Anyway, to replace

MYDBGMACRO("the value of x=%d", x);


MYDBGMACROS("the value of x=%d\n", x);

using Visual Studio's "Find and Replace" dialog, the find expression is:


and the replace expression is:


That replace expression is a little tricky. The goal is to embed a newline in the format string without splitting up the line in the source code. Only the backslash needs to be escaped.

No comments:

Post a Comment