c# - 当 Regex.NextMatch 为空时,程序卡在 NextMatch()
问题描述
我正在尝试解析一个大型 html 文件以提取特定元素,但在解析最后一个元素(Match.NextMatch 为空)之后,它永远不会退出 NextMatch(),代码如下:
Match FrameMatcher = Regex.Match(File.ReadAllText(file.FileName), @"<td class=""tline""><p><a href=""#(.*?)_\[(.*?)]"" style=""font-family:Arial;font-size:10.0pt"">\1 \[\2]<\/a><\/p><\/td>", RegexOptions.Multiline|RegexOptions.Singleline);
int frameCount = 0;
while (FrameMatcher.Success)
{
frameCount++;
FrameMatcher = FrameMatcher.NextMatch();
}
MessageBox.Show(frameCount.ToString());
解决方案
看起来你有一个无限循环。尝试将 while 条件更改为:
while (FrameMatcher != null)
{
frameCount++;
FrameMatcher = FrameMatcher.NextMatch();
}
正如已经提到的,使用 html 解析库而不是正则表达式来解析 html 文件会更好。
推荐阅读
- pytorch - 考虑到自适应学习率,如何使用 Adam optim?
- javascript - 在 React 中处理多个导入的有效方法
- php - 将类似的表单输入存储为数组
- dask - Dask:为可视化()选择什么函数变量最好
- c# - 为什么 OnCollisionEnter2D / OnTriggerEnter2D 不起作用?
- firefox - 在没有隐私通知选项卡的情况下在 Firefox 中启动业力测试并禁用扩展
- amazon-web-services - AWS Route 53:更改自动创建的 NS 记录
- ruby-on-rails - 尝试 Heroku 运行 rake db:使用 cors 迁移时突然出现“配置”错误?
- karate - 支持从场景大纲传递到 JSON 文件
- python - 如何使用 django 创建子类?