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

C#正则表达式匹配缺失头文件失败问题求助

Fixing C# Regex to Extract Missing Header Filenames from Build Logs

Let's break down why your current regex isn't working and fix it to reliably extract the header filenames without their paths.

Issues with Your Original Regex

  1. Missing "file" keyword: Your log lines contain the phrase can't find include file, but your regex only looks for can't find include—this means it's never actually matching the relevant part of the log.
  2. Incorrect character class: The [hed|he|hdb|h] part treats | as a literal character instead of an "or" operator. This would only match single characters like h, e, d, or |, not the full suffixes like .hed or .hdb.
  3. Limited path matching: (\w+/)* uses \w which works for alphanumerics and underscores, but the structure lacks flexibility for optional paths (like the last log line with no path), making it hard to isolate the filename properly.

Corrected Regex and Code

Here's the fixed regex and updated C# code that handles all your log cases:

string[] errLns = System.IO.File.ReadAllLines(logFilePath);
List<string> hdrFiles = new List<string>();
// Regex breakdown:
// - "can't find include file ": Matches the exact error phrase from logs
// - (?:.+/)? : Optional non-capturing group to match any path up to the final /
// - ([^/]+\.h(?:ed|db)?) : Captures the target filename:
//   - [^/]+ : All characters except / (isolates the filename from paths)
//   - \.h(?:ed|db)? : Matches .h, .hed, or .hdb suffixes
string rgxPat = @"can't find include file (?:.+/)?([^/]+\.h(?:ed|db)?)";
Regex incLRgx = new Regex(rgxPat, RegexOptions.IgnoreCase);

foreach (string actLine in errLns)
{
    Match match = incLRgx.Match(actLine);
    // Only add valid matches to avoid empty entries
    if (match.Success)
    {
        hdrFiles.Add(match.Groups[1].Value);
    }
}

How This Works for Your Log Examples

Let's verify against your sample log lines:

  • Line 1: Extracts rsc_interfacestructures_types.h
  • Line 2: Extracts rsc_cpif.h
  • Line 3: Extracts rsc_cpif.h
  • Line 4: Extracts rsc_qm_interfacestructures_types.h

The regex handles both cases where the missing header has a full path and where it's just a filename (no path), and correctly matches all your specified header suffixes.

内容的提问来源于stack exchange,提问作者Béres Zoli

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 06:31:44