You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 01:22:02