获得唯一的字符串C#字符串

2023-09-05 03:34:11 作者:情敌别怂。

我要提取的文件唯一的字符串(* .txt文件)。但我的codeS编写,以便在同一行的再三。我需要发出的每一次唯一的字符串。

I need to extract unique strings from file(*.txt). But my code s written so that the same line s repeated. I need to get each unique string issued once.

我的code:

OpenFileDialog opendialog = new OpenFileDialog();
if (opendialog.ShowDialog() == DialogResult.OK)
{
  var lines = File.ReadLines(opendialog.FileName);
  string pattern = @"set vrouter ""([\w-]+)""";
  foreach (var line in lines)
  {
    var matches = Regex.Matches(line, pattern);
    foreach (Match match in matches)
    {
      if (match.Success)
          textBox1.AppendText(match.Value + '\n');
    }
  }
}

输出:

set vrouter R1
set vrouter R1
set vrouter R2
set vrouter R3
set vrouter R2
set vrouter R4
set vrouter R4
set vrouter R5
set vrouter R1
set vrouter R6
set vrouter R4
set vrouter R3
set vrouter R5

改code:

private void button1_Click(object sender, EventArgs e)
{
  OpenFileDialog opendialog = new OpenFileDialog();
  if (opendialog.ShowDialog() == DialogResult.OK)
  {
    var lines = File.ReadLines(opendialog.FileName);
    string pattern = @"set vrouter ""([\w-]+)""";

    var matches = 
        lines.SelectMany(line=> Regex.Matches(line, pattern)
             .Cast<Match>()).Where(m => m.Success)
             .Select(m => m.Value)
             .Distinct();

    foreach (String match in matches)
    {
      textBox1.AppendText(match + Environment.NewLine);
    }
  }
}

它正常工作!

It work correctly!!!

推荐答案

如果我理解正确,要排除重复。您可以使用 Enumerable.Distinct

If i understand you correctly you want to exclude duplicates. You can use Enumerable.Distinct.

请注意,您需要使用 File.ReadAllLines 而不是 File.ReadLines 如果你要使用的外的foreach ,因为 readlines方法使用它得到一个的StreamReader 引擎盖下设置在第一的foreach 枚举。

Note that you need to use File.ReadAllLines instead of File.ReadLines if you want to use the outer foreach since ReadLines uses a StreamReader under the hood which gets disposed on the first foreach enumeration.

例如:

var matches = Regex.Matches(line, pattern).Cast<Match>()
            .Where(m => m.Success)
            .Select(m => m.Value)
            .Distinct();

foreach (String match in matches)
    textBox1.AppendText(match + Environment.NewLine);

如果您在所有线路(在这里你可以留在 File.ReadLines )想要独特的匹配:

If you want unique matches across all lines(here you can stay with File.ReadLines):

var matches = lines
    .SelectMany(line => Regex.Matches(line, pattern).Cast<Match>())
    .Where(m => m.Success)
    .Select(m => m.Value)
    .Distinct();

要澄清我的第二个方法,这需要无外的foreach,因为在所有的SelectMany 已取代,通过选择所有系列的所有比赛。所以,你只需要一个的foreach 输出的结果。

To clarify my second approach, this requires no outer foreach at all since SelectMany already replaces that by selecting all matches of all lines. So you only need one foreach to output the result.