首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >后臀表达问题。它找不到字符串所需的部分。

后臀表达问题。它找不到字符串所需的部分。
EN

Stack Overflow用户
提问于 2019-07-30 09:53:40
回答 2查看 69关注 0票数 1

我必须解析一个构建日志文件并找到在导出我的工作沙箱时丢失的一些头文件。在C++中,我设法解决了这个问题,但同样的模式对C#无效。

下面是我要解析的行,以获得丢失的头文件的名称:

"Src/EBS\FSW/CustSW/CustSW_generic/RSC/Src/gen/rsc_iohandling_types.h", 第1行:错误(dcc:1621):无法找到包含文件FSW/CustSW/CustSW_plugin/RSC_plugin/RSC_Volvo_QC1/Src/gen/rsc_interfacestructures_types.h "out/VOLVO/QC1/gen/Src/EBS\FSW/CustSW/CustSW_plugin/RSC_plugin/RSC_Volvo_QC1/Src/gen/rsc_b_interfacestructures_types.h", 第19行:错误(dcc:1621):无法找到包含文件FSW/CustSW/CustSW_generic/RSC/Src/gen/rsc_cpif.h "out/VOLVO/QC1/gen/Src/EBS\FSW/CustSW/CustSW_generic/RSC/Src/gen/tvc_safe_types.h", 第19行:错误(dcc:1621):无法找到包含文件FSW/CustSW/CustSW_generic/RSC/Src/gen/rsc_cpif.h "Src/EBS\FSW/CustSW/CustSW_generic/RSC/Src/gen/rsc_iohandling_types.h", 第3行:错误(dcc:1621):找不到包含文件rsc_qm_interfacestructures_types.h

这是当前错误的代码:

代码语言:javascript
复制
string[] errLns = System.IO.File.ReadAllLines(logFilePath);
List<string> hdrFiles = new List<string>();
string rgxPat = @"can't find include (\w+/)*(\w+\.[hed|he|hdb|h])";
Regex incLRgx = new Regex(rgxPat, RegexOptions.IgnoreCase);

foreach (string actLine in errLns)
{
   Match match = incLRgx.Match(actLine);
   hdrFiles.Add(match.Groups[2].Value);
}

我只想拥有没有相对路径的文件名。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-30 09:58:43

你可以用

代码语言:javascript
复制
\bcan't\s+find\s+include\s+file\s+(?:\w+/)*(\w+\.(?:hed?|hdb|h))\b

抓住第一组。见这个regex演示

详细信息

  • \b -字边界
  • can't\s+find\s+include\s+file\s+ - can't find include file,在单词之间和后面加上1+空格
  • (?:\w+/)* - 0+出现的1+单词字符与/后面
  • (\w+\.(?:hed?|hdb|h)) -第1组: 1+ word chars,.,然后是hehedhbdh
  • \b -词边界。

C#代码:

代码语言:javascript
复制
string errLns = System.IO.File.ReadAllText(logFilePath);
List<string> hdrFiles = new List<string>();
string rgxPat = @"\bcan't\s+find\s+include\s+file\s+(?:\w+\/)*(\w+\.(?:hed?|hdb|h))\b";
Regex incLRgx = new Regex(rgxPat, RegexOptions.IgnoreCase);
hdrFiles.AddRange(incLRgx.Matches(errLns).Cast<Match>().Select(x => x.Groups[1].Value).ToArray());
票数 1
EN

Stack Overflow用户

发布于 2019-07-30 10:09:51

试试这个模式can't find include file .+\.(?=hed|he|hdb|h)

can't find include file -匹配can't find include file字面意思

.+ -匹配任何字符中的一个或多个

\. =匹配.字面意思

(?=hed|he|hdb|h) -积极展望-断言下面的是hedhehdbh中的一个

演示

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57268931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档