我正在做最后一年的项目。我有一个包含一些文本的文件。我需要从这个包含“//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;
我找不到我的结果请帮助我。
最佳答案
使用 LINQ
,您只需一行即可完成此操作:
string[] taggedwords = input.Split(' ').Where(x => x.EndsWith(@"//jj")).ToArray();
你所有的//jj 单词都会在那里...
关于c# - 从字符串中获取特定的单词c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34794208/