我正在解析一个文本文件使用
(?<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 条记录的文件需要很长时间才能处理。我在 parse 方法中写了一些控制台输出,发现它似乎在 ~7700 行附近停了将近 10 分钟,然后突然以 500 退出。这是我的 parse 方法(我认为这不重要,但我正在抛出无论如何)
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)]
再次处理文件但这不是最好的解决方案,因为我不再捕获该文件中的额外列,不确定我们是否会使用它,但我宁愿拥有它也不愿不使用它
最佳答案
你使用 .+?
太多了。使用否定字符类并使用 anchor :
(?m)^(?<DateTime>[^\t\r\n]+\t[^\t\r\n]+)\t(?<Data>[^\t\r\n]+)(?:\t(?<Data2>[^\t\r\n]+))?\r?$
参见 proof .
解释
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"
https://stackoverflow.com/questions/63745732/
相关文章:
amazon-web-services - 具有自定义授权方的无服务器 lambda 单元测试处理程
r - 从 R 中的区间 [start, stop] 数据估计密度
post - 错误启动内核 : '_xsrf' argument missing from POST
javascript - 是否可以使用 javascript 将整个网页静音?
apache-spark - 有什么方法可以使用 spark 从 s3 并行读取多个 Parquet
java - 如何在 Java 中测试不同的 "tables input"?
c - 尝试将 'insert' 或 'add' 写入文本文件 - 一个小问题
sql-server - T-SQL Json_modify 将属性附加到每个对象