【发布时间】:2016-12-21 23:04:24
【问题描述】:
我无法匹配 Robocopy 日志文件中的文件名。在 Visual Studio 文本编辑器中制定正则表达式模式时,在 .NET 代码中不起作用。我认为这是因为 StreamReader.Readline() 正在删除 \r (0x0D) 或更改破坏了我编写的模式的行尾。我一直对此耿耿于怀,需要寻求帮助。匹配文件名有两种情况。在第一种情况下:#1 文件名后面有一个 \r (0x0D),其他情况下:#2 文件名后面有一个 \t。
在 Visual Studio 2015 中,以下模式适用于示例中的所有文件名,但在 .NET 中,.DS_Store 上没有匹配项:
(?<=New File.+)(?<=\d\t)(.*?\t|.*?\r)
日志文件:
New Dir 7 \\vboxsvr\win8devdocs\pictures\
New File 8196 .DS_Store
0%
100%
New File 6607 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.txt 17:58 -> 17:58
0%
100%
New File 1198408 IMG_20131124_125621_954.jpg 17:58 -> 17:58
0%
21%
43%
65%
87%
100%
New File 1132046 IMG_20131212_104250_300.jpg 17:58 -> 17:58
读取 Robocopy 文件并保存复制文件的文件名和目录的方法(但不处理复制失败):
private void LogCopiedFiles(string absRoboLogPath, string absFileCopyListLog) {
try {
UTF8Encoding encoder = new UTF8Encoding(true);
using(FileStream write = File.OpenWrite(absFileCopyListLog))
using(StreamReader read = File.OpenText(absRoboLogPath)) {
string currentDir = string.Empty;
while(!read.EndOfStream) {
string line = read.ReadLine();
Match newDir = findDirNamesInLog.Match(line);
Match newFile = findFileNamesInLog.Match(line);
if(newDir.Success) {
currentDir = newDir.Value;
}
else if(newFile.Success) {
byte[] byteLine = encoder.GetBytes(string.Concat(currentDir, newFile.Value, Environment.NewLine));
write.Write(byteLine, 0, byteLine.Length);
}
}
}
}
catch(Exception ex) {
}
}
我可以使用什么模式来仅从 .NET 的日志中获取文件名?
【问题讨论】:
-
仅供参考,此
(?<=New File.+)(?<=\d\t)与(?<=New File.*\d\t)相同。虽然没有解决这个问题。 -
啜饮在这里是一个技术术语吗?
-
没那么多。它的 Perl 语言意味着获取。几年前我在代码注释中看到它并卡住了。