问题描述:

I need to append \n to my TRACE statement in a visual studio 2012 c++ project. That is to replace statements such as TRACE("test an"); to TRACE("test an\n");.

I haved tried regex (TRACE\(\".*([^\\][^n]))\" and $1\\n"

But there are two problems.

1) the regex does not match string whose last character is n, but the second-last character is not \.

e.g. TRACE("haha an") does not match.

2) the replacement string \\n does not escape \\ to \, the replaced string would contain two \.

Could you help me. thx.

网友答案:
Regex.Replace(text, "(TRACE\\(.*(?<!\\\\n))(\"\\))", "$1\\n$2");

Need to use "Zero-width negative lookbehind assertion.", see HERE

网友答案:

Try regex:

(TRACE\(")(.*)("\))

with replacement:

$1$2\\n$3

But this will add \n even if \n already exists. To fix this you can use regex:

(TRACE\(")(.*?)(\\n)*("\))

with replacement:

$1$2\\n$4
相关阅读:
Top