如何用.NET(C#)正则匹配if(Core_Debug)外的printf()
需求:用.NET(C#)正则匹配if(Core_Debug)外部的printf()
我需要编写.NET(C#)正则表达式,在数百万行C++代码中匹配所有位于if(Core_Debug)语句之外的printf()。这些if语句格式完全不统一,部分包含多个printf()。
此前尝试的正则存在的问题
- 会忽略任何紧跟
}(忽略空格)的printf(); - 在本地C#程序中运行时,处理部分文件会卡顿,尽管在正则测试工具上运行正常;
- 偶尔会误判嵌套在
if(Core_Debug)内的printf()为外部匹配,测试工具上无此问题。
后续尝试非捕获组和条件匹配的正则,仅能匹配if(Core_Debug)内部的printf(),无法筛选出外部的目标。
测试代码示例
if ( Core_Debug ) printf("Query Thread ID = %x\n", GetCurrentThreadId()); if ( Core_Debug ) { printf("Query Thread ID = %x\n", GetCurrentThreadId()); //This should be negated by negative lookahead } if ( Core_Debug ) { printf("Query Thread ID = %x\n", GetCurrentThreadId()); printf("Query Thread ID = %x\n", GetCurrentThreadId()); printf("%lg\n", value); printf("Query Thread ID = %x\n", GetCurrentThreadId()); //\<- I want this to not match, it currently does not } if(Core_Debug){ printf("Query Thread ID = %x\n", GetCurrentThreadId()); printf("Query Thread ID = %x\n", GetCurrentThreadId()); printf("Query Thread ID = %x\n", GetCurrentThreadId()); //<- I want this to not match, it currently does not } if(Core_Debug) { printf("%lg\n", value); printf("%lg\n", value); printf("%lg\n", value); printf("%lg\n", value); } if ( Core_Debug ) printf("****type_of_service=%s\n", (char*)k_type_of_service); if ( Core_Debug ) { printf("Enable Table called\n"); //printf("Enable Table called\n"); //This should be negated by negative lookahead } printf("blah blah"); printf("blah blah"); //some important comment documenting everything printf("blah blah"); sprintf(blah,"blah blah"); nprintf(blah,"blah blah"); xprintf(blah,"blah blah"); fprintf(blah,"blah blah"); cdprintf(blah,"blah blah"); goboldiegook(); //printf("blah blah"); goboldiegook(){ something; printf("blah blah"); something; printf("blah blah"); printf(); something; printf(); // \<- I want this to match, it currently does not } if (Core_Debug) { if (hw_present) printf("%s: HW is Present\n", mod_name.c_str()); else printf("%s: HW is NOT Present\n", mod_name.c_str()); if (sw_debug) printf("%s: SW Debug IS enabled\n", mod_name.c_str()); else printf("%s: SW Debug is NOT enabled\n", mod_name.c_str()); //<- I want this to not match, it currently does not }
核心诉求
寻求一种适配无固定格式场景的.NET(C#)正则实现,准确区分内外的printf(),例如:
if (Core_Debug) { printf("something"); //<- 不匹配 printf("something"); //<- 不匹配 } printf("something"); //<- 匹配
内容的提问来源于stack exchange,提问作者lht21
相关产品推荐
相关产品推荐

