C# 如何在字符串末尾包含名称的特定部分
今天早些时候我提出了这个问题,但我删除了它,因为我犯了一个错误,没有提供所有信息。很抱歉。 我使用Regex和Linq搜索文件夹中与模式匹配的所有.txt文件。这部分代码如下所示:C# 如何在字符串末尾包含名称的特定部分,c#,regex,C#,Regex,今天早些时候我提出了这个问题,但我删除了它,因为我犯了一个错误,没有提供所有信息。很抱歉。 我使用Regex和Linq搜索文件夹中与模式匹配的所有.txt文件。这部分代码如下所示: private static IEnumerable<string> Search(string file) { return File .ReadLines(file) .Select(line =&
private static IEnumerable<string> Search(string file)
{
return File
.ReadLines(file)
.Select(line => Regex.Match(line, pattern))
.Where(match => match.Success)
.Select(match => match.Value)
.ToArray();
}
是否仍然需要写入匹配文件的名称?我有一个字符串数组中的文件名
var filenames = Directory.GetFiles(filePath, "*.txt")
.Select(filename => Path.GetFileNameWithoutExtension(filename))
.Select(filename => Regex.Match(filename, namePattern))
.Where(match => match.Success)
.Select(match => match.Value)
.ToArray();
我的目标“results.txt”示例:
示例1file1
示例2file2
示例3file3
“例子”是已经在工作的部分,highlits是我想以某种方式实现的
勒内·沃格特之前很友好地给了我一个答案,但经过多次尝试,我仍然无法解决这个问题。他的密码是:
var extracts = files.SelectMany(file => Search(file).Select(match => new {match, file}));
File.WriteAllLines("results.txt",
extracts.Select(extract => extract.match + " " + extract.file));
它给了我这样一个结果:
示例1d:\~.txt1
有没有什么方法可以用我的名字模式来删掉不必要的部分
非常感谢你
编辑:感谢迄今为止的所有帮助!我正在尝试所有的答案。namePattern与模式不同。我使用模式从文本中获取重要字符串,并使用namePattern剪切名称中不必要的部分。我可以得到我想要的文件名部分,我正在努力处理结果文本文件。我只能在没有模式的情况下写出整个文件名,或者每次尝试以某种方式包含模式时都失败了
static string namePattern = @"(\d{4})(?!.*\d)";
static string pattern = @"[A-Z]{1}\d{7}\s\d{1,5}";
您可以使用这段代码。它将删除最后一个反斜杠之前的所有内容,包括反斜杠本身
string filename = Regex.Match(filename, @".*\\([^\\]+$)").Groups[1].Value;
您可以使用这段代码。它将删除最后一个反斜杠之前的所有内容,包括反斜杠本身
string filename = Regex.Match(filename, @".*\\([^\\]+$)").Groups[1].Value;
如果要将文件名追加到每行
private static IEnumerable<string> Search(string file)
{
return File
.ReadLines(file)
.Select(line => Regex.Match(line, pattern))
.Where(match => match.Success)
.Select(match => match.Value + " " + file)
.ToArray();
}
私有静态IEnumerable搜索(字符串文件)
{
返回文件
.ReadLines(文件)
.Select(line=>Regex.Match(line,pattern))
.Where(匹配=>match.Success)
.Select(match=>match.Value+“”+文件)
.ToArray();
}
如果要将文件名附加到单独的行中
private static IEnumerable<string> Search(string file)
{
return (File
.ReadLines(file)
.Select(line => Regex.Match(line, pattern))
.Where(match => match.Success)
.Select(match => match.Value)
)
.Concat(Enumerable.Repeat(file, 1))
.ToArray();
}
私有静态IEnumerable搜索(字符串文件)
{
返回(文件)
.ReadLines(文件)
.Select(line=>Regex.Match(line,pattern))
.Where(匹配=>match.Success)
.Select(匹配=>match.Value)
)
.Concat(可枚举的.Repeat(文件,1))
.ToArray();
}
由于这些方法返回一个
IEnumerable
,因此您还可以删除ToArray()
。这会导致表达式被延迟计算,即在File.writeAllines
中调用它时。如果文件非常大或有许多文件,这可能是一个优势,因为结果不会被缓冲。读取的每一行都将立即写入输出。如果希望将文件名附加到每一行
private static IEnumerable<string> Search(string file)
{
return File
.ReadLines(file)
.Select(line => Regex.Match(line, pattern))
.Where(match => match.Success)
.Select(match => match.Value + " " + file)
.ToArray();
}
私有静态IEnumerable搜索(字符串文件)
{
返回文件
.ReadLines(文件)
.Select(line=>Regex.Match(line,pattern))
.Where(匹配=>match.Success)
.Select(match=>match.Value+“”+文件)
.ToArray();
}
如果要将文件名附加到单独的行中
private static IEnumerable<string> Search(string file)
{
return (File
.ReadLines(file)
.Select(line => Regex.Match(line, pattern))
.Where(match => match.Success)
.Select(match => match.Value)
)
.Concat(Enumerable.Repeat(file, 1))
.ToArray();
}
私有静态IEnumerable搜索(字符串文件)
{
返回(文件)
.ReadLines(文件)
.Select(line=>Regex.Match(line,pattern))
.Where(匹配=>match.Success)
.Select(匹配=>match.Value)
)
.Concat(可枚举的.Repeat(文件,1))
.ToArray();
}
由于这些方法返回一个IEnumerable
,因此您还可以删除ToArray()
。这会导致表达式被延迟计算,即在File.writeAllines
中调用它时。如果文件非常大或有许多文件,这可能是一个优势,因为结果不会被缓冲。读取的每一行都将立即写入输出。尝试以下操作:
string[] filenames = Directory.GetFiles(filePath, "*.txt")
.Where(x => Regex.IsMatch(Path.GetFileNameWithoutExtension(x), namePattern))
.Select(x => string.Format("***{0}***", x)).ToArray();
请尝试以下操作:
string[] filenames = Directory.GetFiles(filePath, "*.txt")
.Where(x => Regex.IsMatch(Path.GetFileNameWithoutExtension(x), namePattern))
.Select(x => string.Format("***{0}***", x)).ToArray();
使用此方法:
private string GetFileName(string file)
{
return file.Split('\\').Last();
}
然后这个:
private static IEnumerable<string> Search(string file)
{
return File
.ReadLines(file)
.Select(line => Regex.Match(line, pattern))
.Where(match => match.Success)
.Select(match => match.Value+" "+GetFileName(file))
.ToArray();
}
私有静态IEnumerable搜索(字符串文件)
{
返回文件
.ReadLines(文件)
.Select(line=>Regex.Match(line,pattern))
.Where(匹配=>match.Success)
.Select(match=>match.Value+“”+GetFileName(文件))
.ToArray();
}
你会得到你想要的
或者,直接:
private static IEnumerable<string> Search(string file)
{
return File
.ReadLines(file)
.Select(line => Regex.Match(line, pattern))
.Where(match => match.Success)
.Select(match => match.Value+" "+file.Split('\\').Last())
.ToArray();
}
私有静态IEnumerable搜索(字符串文件)
{
返回文件
.ReadLines(文件)
.Select(line=>Regex.Match(line,pattern))
.Where(匹配=>match.Success)
.Select(match=>match.Value+“”+file.Split(“\\”).Last()
.ToArray();
}
使用此方法:
private string GetFileName(string file)
{
return file.Split('\\').Last();
}
然后这个:
private static IEnumerable<string> Search(string file)
{
return File
.ReadLines(file)
.Select(line => Regex.Match(line, pattern))
.Where(match => match.Success)
.Select(match => match.Value+" "+GetFileName(file))
.ToArray();
}
私有静态IEnumerable搜索(字符串文件)
{
返回文件
.ReadLines(文件)
.Select(line=>Regex.Match(line,pattern))
.Where(匹配=>match.Success)
.Select(match=>match.Value+“”+GetFileName(文件))
.ToArray();
}
你会得到你想要的
或者,直接:
private static IEnumerable<string> Search(string file)
{
return File
.ReadLines(file)
.Select(line => Regex.Match(line, pattern))
.Where(match => match.Success)
.Select(match => match.Value+" "+file.Split('\\').Last())
.ToArray();
}
私有静态IEnumerable搜索(字符串文件)
{
返回文件
.ReadLines(文件)
.Select(line=>Regex.Match(line,pattern))
.Where(匹配=>match.Success)
.Select(match=>match.Value+“”+file.Split(“\\”).Last()
.ToArray();
}
也许您需要var extracts=files.SelectMany(file=>$“{Search(file)}{System.IO.Path.GetFileNameWithoutExtension(file)}”)代码>可以在正则表达式模式中使用分组。你现在的图案是什么样子?我在你的问题中找不到。@itsme86我已经更新了帖子。谢谢也许您需要var extracts=files.Selec