【发布时间】:2021-04-10 18:12:34
【问题描述】:
这个应该很简单,但是难住了。
我有一个可以匹配的正则表达式。如果是这样,我想知道它在哪条线上。有没有一种简单的方法可以用 Linq 做到这一点,而不用遍历每一行来计算字符?
Regex rx = new Regex(myRegEx, RegexOptions.Compiled | RegexOptions.IgnoreCase);
string[] docLines = File.ReadAllLines(myDocPath);
// Find matches
MatchCollection matches = rx.Matches(string.Join(Environment.NewLine, docLines));
if(matches.Count > 0)
{
long loc = matches[0].Index;
//Find the Line
}
【问题讨论】:
-
请注意,Regex 或 LINQ 无论如何都会遍历字符和行。