我已经将文本文件与“..”之类的模式进行了比较,我只需要在文本文件中写入不同的值...
while ((line = file.ReadLine()) != null)
{
foreach (Match match in Regex.Matches(line, @"(\w*)\.\."))
{
dest.WriteLine(match.Groups[1]);
}
counter++;
}
如何获得不同的值...有什么建议吗?
最佳答案
将匹配项添加到列表(如果尚未添加)?或者只是保留已添加内容的运行列表?一些像:
List<string> seen = new List<string>();
string line = string.Empty;
while ((line = file.ReadLine()) != null)
{
foreach (Match match in Regex.Matches(line, @"(\w*)\.\."))
{
if (!seen.Contains(line))
{
Console.WriteLine(line);
seen.Add(line);
}
}
}
编辑:我在这里解释了你的意思;如果您真的想要匹配组值,请将条件 block 中的行替换为 match.Groups[1].Value ...
关于c# - 将文本文件与 C# 中的文本模式进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4458639/