正则表达式以匹配任何行上的多个 MBCS 字符串

Regex to match multiple MBCS strings on any line

本文关键字:MBCS 字符串 任何行 正则表达式      更新时间:2023-10-16

我正在寻找一个正则表达式来仅匹配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""单词上不匹配,但仍然匹配它们以吃结束引号而不将其作为匹配项返回?

编辑:(?:_T("[^"]+").*?|L"[^"]+".*?)*(?<!#include )(?<!_T()(?<!L)(?<!\)"(.*?)"(?<!\"),这个正则表达式几乎可以完成这项工作,但还有一个测试用例失败了,我最初没有想到要包括。

_T("don't match this") + _T("or this"); // shouldn't match anything, matches ") + _T("

您实际上可能会匹配_TL部分,以便在上一个匹配中消耗它们:

(?:_T("[^"]+").*?|L"[^"]+".*?)?(?<!#include )(?<!_T(|L|\)"(.*?)"(?<!\")

我还缩短了负面的回望。

正则表达式101演示