我正在寻找一个正则表达式,它只匹配C++项目中的MBCS字符串。这些字符串包含在双引号中,没有L"..."或_T("...")说明符。在任何代码行上都可以有多个引号。字符串可以包含不应该结束匹配的转义子字符串。以下是几个例子:
"This is a MBCS string"; // "This is a MBCS string" match
_T("This is maybe a unicode string"); // no match
L"This is a unicode string"; // no match
"These both" + "should match"; // "These both" and "should match" match
"This is a \"quoted\" string"; // "This is a \"quoted\" string" match我有一个正则表达式,不能用负回溯(?<!#include )(?<!_T\()(?<!\\)(?<!L)\"(.*?)\"(?<!\\\")来处理这一切,但是它变得更复杂了。在一行中,字符串类型的混合开始出现问题。
_T("Maybe this") + "is a match"; // "is this" match but instead would match ") + "
do_something(_T("This doesn't match")) + do_something("but this does match"); // "but this does match" match but instead it matches ")) + do_something("如何才能使正则表达式在_T("")和L""单词上不匹配,但仍然匹配它们以吃掉末尾引号而不将其作为匹配项返回呢?
编辑:这个regex,(?:_T\(\"[^\"]+\"\).*?|L\"[^\"]+\".*?)*(?<!#include )(?<!_T\()(?<!L)(?<!\\)\"(.*?)\"(?<!\\\"),几乎完成了这项工作,但是还有一个测试用例失败了,这是我最初没有想到的。
_T("don't match this") + _T("or this"); // shouldn't match anything, matches ") + _T("发布于 2013-10-01 20:19:26
您可能实际上匹配了_T和L部件,以便在以前的匹配中使用它们:
(?:_T\(\"[^\"]+\"\).*?|L\"[^\"]+\".*?)?(?<!#include )(?<!_T\(|L|\\)\"(.*?)\"(?<!\\\")我还缩短了负值的后面。
regex101演示
https://stackoverflow.com/questions/19125012
复制相似问题