[英]Replacing / removing specific part of file names using regex
I'm new to C# and need to loop through a directory and remove the last part of any and all files that end in _xxxxxx.xml
(an underscore followed by 6 digits).我是 C# 的新手,需要遍历目录并删除以
_xxxxxx.xml
结尾的所有文件的最后一部分(下划线后跟 6 位数字)。
For example, the filename filename_A_123_456789.xml
becomes filename_A_123.xml
.例如,文件名
filename_A_123_456789.xml
变为filename_A_123.xml
。
What I have so far:到目前为止我所拥有的:
private static void RenameFiles(string outputPath)
{
DirectoryInfo d = new DirectoryInfo(outputPath);
FileInfo[] infos = d.GetFiles();
foreach (FileInfo f in infos)
{
string newName = Regex.Replace(f.FullName, "(\\[_\d]\\s)", temp);
File.Move(f.FullName, newName);
}
}
But that's not working.但这行不通。 Ang CSharp Regexxers out there?
Ang CSharp Regexxers 在那里?
You can use the regex:您可以使用正则表达式:
_\d{6}(\.[^.]+)$
and replace with $1
instead.并用
$1
代替。
The regex is matching 6 digits, then group 1 ( (\.[^.]+)
) matches the extension, which you replace with in the replacement string.正则表达式匹配 6 位数字,然后第 1 组 (
(\.[^.]+)
) 匹配您在替换字符串中替换的扩展名。 The extension is matched by "a dot followed by a bunch of non-dots".扩展名由“一个点后跟一堆非点”匹配。 Also note that the end of string anchor
$
to assert that all of this must be at the end of the string.还要注意字符串的末尾锚
$
来断言所有这些都必须在字符串的末尾。
Change your code to:将您的代码更改为:
string newName = Regex.Replace(f.FullName, @"_\d{6}(\.[^.]+)$", "$1");
A regex is fine for your need.正则表达式可以满足您的需要。 As an alternative, here is a regex free solution if you want to use one:
作为替代方案,如果您想使用一个无正则表达式的解决方案:
public static void Main()
{
var source = @"filename_A_123_456789.xml";
var slices = Path.GetFileNameWithoutExtension(source).Split('_');
var last = slices.Last();
var isSpecific = last.Length == 6 && last.All(char.IsDigit);
var result = string.Join("_", isSpecific ? slices.Take(slices.Length - 1) : slices) + Path.GetExtension(source);
Console.WriteLine(result);
}
Your regex doesn't quantify the \d
in any way to define that you are looking for 6 digits to be exact.您的正则表达式不会以任何方式量化
\d
来定义您正在寻找准确的 6 位数字。
Use this instead:改用这个:
(\S*)_\d{6}(\.xml) /m
This regex captures all non-whitespace characters before the _
followed by six digits, and the extension .xml
into two separate capturing groups.此正则表达式将
_
之前的所有非空白字符,后跟六位数字和扩展名.xml
捕获到两个单独的捕获组中。
Then replace with $1$2
, group 1 followed by group 2.然后替换为
$1$2
,第 1 组,然后是第 2 组。
Just try:试试看嘛:
string newName = Regex.Replace(f.FullName, "(\\_\\d{6}\\.xml)", ".xml");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.