Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#RegEx比赛在哪一行进行。林克?_C#_Regex_Linq - Fatal编程技术网

c#RegEx比赛在哪一行进行。林克?

c#RegEx比赛在哪一行进行。林克?,c#,regex,linq,C#,Regex,Linq,这个应该很简单,但它难住了我 我有一个正则表达式可以匹配。如果有,我想知道它在哪条线上。有没有一种简单的方法可以做到这一点,也许是使用Linq,而不必在每行循环计数字符 Regex rx = new Regex(myRegEx, RegexOptions.Compiled | RegexOptions.IgnoreCase); string[] docLines = File.ReadAllLines(myDocPath); // Find matches MatchCollec

这个应该很简单,但它难住了我

我有一个正则表达式可以匹配。如果有,我想知道它在哪条线上。有没有一种简单的方法可以做到这一点,也许是使用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 rx = new Regex(myRegEx, RegexOptions.Compiled | RegexOptions.IgnoreCase);
  string[] docLines = File.ReadAllLines(myDocPath);
  // Find matches
  for(int x = 0; x < docLines.Length; x++){
    string line = docLines[x];
    if(rx.IsMatch(line))
      Console.Write($"Match on line {x}");
  }
Regex rx=new Regex(myRegEx,RegexOptions.Compiled | RegexOptions.IgnoreCase);
string[]docLines=File.ReadAllLines(myDocPath);
//查找匹配项
对于(int x=0;x
您可以使用
匹配值来查找行,例如:

Regex rx = new Regex("b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
string[] docLines = new[] { "zzazz", "zzbzz", "zzczz", "zzzzz" };
// Find matches
MatchCollection matches = rx.Matches(string.Join(Environment.NewLine, docLines));
if (matches.Count > 0)
{
    string loc = matches[0].Value;
    //Find the Line
    var line = docLines.FirstOrDefault(x => x.Contains(loc));
}
Runfastman

您可以通过使用LINQ并结合索引来实现这一点,如下所示:

using System.Linq;
using System.Text.RegularExpressions;
   
var rx = new Regex(myRegEx, RegexOptions.Compiled | RegexOptions.IgnoreCase);
var docLines = File.ReadAllLines(myDocPath);
var matchLine = docLines.Select((line, index) => new { line, index }).FirstOrDefault(l => rx.IsMatch(l.line));
if(matchLine != null) Console.WriteLine($@"Match line # = {matchLine.index}");
虽然编译器确实会遍历这些行来完成任务(正如其他人所提到的),但我感觉上面的内容就是您所想到的

编辑

根据您关于目标字符串可能跨越多行的评论,我将执行以下操作:

var rx = new Regex(myRegEx, RegexOptions.Compiled | RegexOptions.IgnoreCase);
//Read all text into a variable.
var docLines = File.ReadAllText(myDocPath);
//Check that there is, in fact, a match.
if(!rx.IsMatch(docLines)) return;
//Split your text blob by that match.
var rxSplit = rx.Split(docLines);
//Then count the number of line brakes before the match.
var startOfMatchLine = new Regex(@"(\n|\r\n?)").Matches(rxSplit[0]).Count;
//And print the result.
Console.WriteLine($@"{startOfMatchLine}");

请注意,Regex或LINQ无论如何都会在字符和行之间循环。如果Regex跨越多行,它将找不到匹配项。请更具体地说明示例数据和Regex模式,以便将此要求考虑在内?我认为如果Regex跨越多行,这也会有问题,那它就找不到了。我对LINQ不太在行,但在它到达正确的索引之前,它不能计算每行的字符数吗?@runfastman,我已经根据你的评论更新了我的答案。我甚至没有想到仅仅计算换行符,这么简单。谢谢