如何在字符串末尾包含文件名

时间:2018-07-11 14:02:34

标签: c# regex

我使用Regex和Linq搜索与模式匹配的文件夹中的所有.txt文件。该部分代码如下所示:

--config GDAL_PDF_LAYERS_OFF

然后将匹配项写入.txt:

select * into newTable
from
(
select UserId, Address1Line as Address, Address1City as City, Address1State as State
from myTable
union all
select UserId, Address2Line as Address, Address2City as City, Address2State as State
from myTable
) tmp

总有写匹配来源的文件名吗?我在字符串数组中有文件名。

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();
        }

我的目标“ results.txt”示例:

var files = Directory.EnumerateFiles(filePath, "*.txt");
var extracts = files.SelectMany(file => Search(file));
File.WriteAllLines("results.txt", extracts);

“示例”是已经起作用的部分,高亮部分是我想以某种方式实现的。即使我必须重写整个代码,也将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:1)

您可以将文件名直接附加到匹配项中:

var extracts = files.SelectMany(file => Search(file).Select(line => line + " " + file));
File.WriteAllLines("results.txt", extracts);

如果需要在不包含文件的情况下保持干净的“提取物”,则可以创建匿名对象:

var extracts = files.SelectMany(file => Search(file).Select(match => new {match, file}));
File.WriteAllLines("results.txt", 
                   extracts.Select(extract => extract.match + " " + extract.file));

答案 1 :(得分:0)

我会这样写:

var matches = Directory.EnumerateFiles(filePath, "*.txt")
.Where(currentPath => Regex.IsMatch(Path.GetFileNameWithoutExtension(currentPath), pattern))
.AsParallel() // useful if there are many files
.Select(currentPath => 
    new {FilePath = filePath, MatchingValue = File.ReadLines(currentPath)
        .Select(line => Regex.Match(line, pattern))
        .Where(m => m.Success)
        .FirstOrDefault()?.Value
})
.Where(m => !string.IsNullOrEmpty(m.MatchingValue))
.Select(m => $"{m.MatchingValue} {Path.GetFileName(m.FilePath)}");

File.WriteAllLines(@"results.txt", matches);