我的简单正则表达式是否出现了灾难性回溯?

3

我正在使用

解析文本文件
(?<DateTime>.+?\t.+?)\t(?<Data>.+?)(\t(?<Data2>.+?))?\r\n

最初它只是

(?<DateTime>.+?\t.+?)\t(?<Data>.+?)\r\n

但后来我发现有一个文件有一个额外的列需要在这个API解析的10个文件中加以考虑,所以我必须编辑它以得出第一个正则表达式。

这是我正在解析的数据的示例。

2020-05-26  08:30:06    18.6
2020-05-26  08:44:38    18.0
2020-05-26  08:52:04    17.5
2020-05-26  09:17:44    18.0
2020-05-26  10:25:35    17.5
2020-05-26  10:47:08    18.0
2020-05-26  11:06:08    18.5

以下是含有异常列的数据

2019-08-21  10:32:21    0   00000   
2019-08-21  19:21:37    0   00000   
2019-08-21  23:24:10    0   00000   
2019-08-22  00:47:39    0   00000   

请注意,虽然现在这些值都是零,但其他值也是可能的。
现在所有的东西都还在“工作”,但在我对正则表达式进行编辑后,有一个包含约8000条记录的文件需要花费很长时间来处理。我在解析方法中编写了一些控制台输出,并发现它似乎在第7700行附近停止了将近10分钟,然后突然以500的状态代码退出。这是我的解析方法(我不认为这很重要,但我还是把它放进来了)。
DataRow row;
index = 0;
Console.WriteLine("Beginning parse loop");
foreach (Match match in reg.Matches(data)) {
    row = table.NewRow();
    foreach (List<string> column in columns) {
        string value = getRegexGroupValue(match, column);
        if (column[1] == "System.DateTime") {
           if (value != "") {
              row[column[0]] = Convert.ToDateTime(value);
           }
        } else if (column[1] == "System.Int32") {
            row[column[0]] = Convert.ToInt32(value);
        } else {
            row[column[0]] = value;
        }
    }

    table.Rows.Add(row);
    Console.WriteLine(String.Format("Ending loop {0}", index++));
}

这里发生了什么?
当我在调试控制台中使用reg.Matches(data).Count时,它报错了并没有显示行数,但是当我使用Notepad ++检查正则表达式时,可以很好地得到总行数。
编辑:我再次处理文件时使用了(?<DateTime>.+?\t.+?)\t(?<Data>.+?)[(\t)(\r\n)]但这不是最好的解决方案,因为我不再捕获该文件中的额外列,不确定我们是否会使用该列,但我宁愿有而不是没有。

1
你丢失了 ?: (<Data2>.+?) => (?<Data2>.+?) - Wiktor Stribiżew
2
似乎按空格分割会更容易。 - ggorlen
好的,我已经创建了一个解决正则表达式的方法,并且必须重写原始代码,看起来我掉了我的“?”。 - DreadedEntity
1
正如@ggorlen所说,只需在空格(在这种情况下为\t)上进行分割并处理各个元素,这将会更加容易。您是否有使用正则表达式的非常好的理由? - Pranav Hosangadi
我不知道这是否算作一个“非常好”的理由,但使用命名捕获组让我在代码的其余部分拥有了极大的灵活性,此外还有一种简化、标准化的解析文本文件的方式(这只是15个以上ETL API中的1个)。我的另一个选择是循环遍历输入文件中的每一行,并编写手动解析每种类型文件的代码,我想不出一种简单和标准的方法来做到这一点。这支持我接下来的步骤,将所有这些内容移植到一个共享的“ETL”类中,所有API都将使用它,而不是将所有代码复制粘贴到它们中。 - DreadedEntity
1个回答

0

你正在过度使用.+?。请使用否定字符类并使用锚点:

(?m)^(?<DateTime>[^\t\r\n]+\t[^\t\r\n]+)\t(?<Data>[^\t\r\n]+)(?:\t(?<Data2>[^\t\r\n]+))?\r?$

请查看证明

解释

                           EXPLANATION
--------------------------------------------------------------------------------
  (?m)                     set flags for this block (with ^ and $
                           matching start and end of line) (case-
                           sensitive) (with . not matching \n)
                           (matching whitespace and # normally)
--------------------------------------------------------------------------------
  ^                        the beginning of a "line"
--------------------------------------------------------------------------------
  (?<DateTime>             group and capture to \k<DateTime>:
--------------------------------------------------------------------------------
    [^\t\r\n]+               any character except: '\t' (tab), '\r'
                             (carriage return), '\n' (newline) (1 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    \t                       '\t' (tab)
--------------------------------------------------------------------------------
    [^\t\r\n]+               any character except: '\t' (tab), '\r'
                             (carriage return), '\n' (newline) (1 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \k<DateTime>
--------------------------------------------------------------------------------
  \t                       '\t' (tab)
--------------------------------------------------------------------------------
  (?<Data>                  group and capture to \k<Data>:
--------------------------------------------------------------------------------
    [^\t\r\n]+               any character except: '\t' (tab), '\r'
                             (carriage return), '\n' (newline) (1 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \k<Data>
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    \t                       '\t' (tab)
--------------------------------------------------------------------------------
    (?<Data2>              group and capture to \k<Data2>:
--------------------------------------------------------------------------------
      [^\t\r\n]+               any character except: '\t' (tab), '\r'
                               (carriage return), '\n' (newline) (1
                               or more times (matching the most
                               amount possible))
--------------------------------------------------------------------------------
    )                        end of \k<Data2>
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  \r?                      '\r' (carriage return) (optional (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of a
                           "line"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接