我有一个看起来像这样的字符串变量 -
string Output = "C:\src\Hello1.txt -> C:\Temp\hewvjke\Hello1.txt
C:\src\Hello.txt -> C:\Temp\hewvjke\Hello.txt
2 File(s) copied";
基本上它包含"<source file> -> <destination file>"
n次。
我想要一个可以提取目标文件列表的正则表达式。
我希望输出变量是这样的
string afterRegex = "C:\Temp\hewvjke\Hello1.txt
C:\Temp\hewvjke\Hello.txt
2 File(s) copied"
我该怎么做?
答案 0 :(得分:2)
问题:我认为你有一个文件ex:myfile
包含以下3行
==source path== ==destination path==
C:\src\Hello1.txt -> C:\Temp\hewvjke\Hello1.txt
C:\src\Hello.txt -> C:\Temp\hewvjke\Hello.txt
2 File(s) copied
您要从上述文件内容中删除源路径 所以预期的产出应该如下:
C:\Temp\hewvjke\Hello1.txt
C:\Temp\hewvjke\Hello.txt
2 File(s) copied
<强>程序:强>
String[] lines = System.IO.File.ReadAllLines("myfile.txt");
String[] newLines = new String[lines.Length];
int i=0;
foreach (String line in lines)
{
newLines[i]=(line.Contains(">"))?line.Split('>')[1]:line;
i++;
}
System.IO.File.WriteAllLines("myfile.txt",newLines);
输出执行上述语句后myfile
包含以下文字:
C:\Temp\hewvjke\Hello1.txt
C:\Temp\hewvjke\Hello.txt
2 File(s) copied
注意:如果您想要做其他事,请告诉我。
答案 1 :(得分:0)
考虑以下代码......
string inputMessage = @"C:\src\Hello1.txt -> C:\Temp\hewvjke\Hello1.txt
C:\src\Hello.txt -> C:\Temp\hewvjke\Hello.txt
2 File(s) copied";
string outputMessage = Regex.Replace(inputMessage, @".* -> " , "");
祝你好运!