从字符串c#中获取特定单词

Sha*_*fiq 2 .net c# regex string

我正在做一个最后一年的项目.我有一个包含一些文本的文件.我需要从包含"// jj"标签的文件中获取单词.例如abc // jj,bcd // jj等

假设文件包含以下文本

ffafa adada // bb adad ssss // jj aad adad adadad aaada dsdsd // jj dsdsd sfsfhf // vv dfdfdf

我需要与// jj标签相关的所有单词.这几天我被困在这里.我正在尝试的代码

  // Create OpenFileDialog
        Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

        // Set filter for file extension and default file extension
        dlg.DefaultExt = ".txt";
        dlg.Filter = "Text documents (.txt)|*.txt";

        // Display OpenFileDialog by calling ShowDialog method
        Nullable<bool> result = dlg.ShowDialog();

        // Get the selected file name and display in a TextBox
        string filename = string.Empty;
        if (result == true)
        {
            // Open document
            filename = dlg.FileName;
            FileNameTextBox.Text = filename;
        }

        string text;
        using (var streamReader = new StreamReader(filename, Encoding.UTF8))
        {
            text = streamReader.ReadToEnd();
        }

        string FilteredText = string.Empty;

        string pattern = @"(?<before>\w+) //jj (?<after>\w+)";

        MatchCollection matches = Regex.Matches(text, pattern);

        for (int i = 0; i < matches.Count; i++)
        {
            FilteredText="before:" + matches[i].Groups["before"].ToString();
            //Console.WriteLine("after:" + matches[i].Groups["after"].ToString());
        }

        textbx.Text = FilteredText;
Run Code Online (Sandbox Code Playgroud)

我找不到我的结果请帮助我.

Ian*_*Ian 7

有了LINQ你可以用一条线做到这一点:

string[] taggedwords = input.Split(' ').Where(x => x.EndsWith(@"//jj")).ToArray();
Run Code Online (Sandbox Code Playgroud)

你所有的// jj词都会在那里......