基于文件夹名称复制文件的良好Regex

本文关键字:Regex 文件 复制 文件夹 | 更新日期: 2023-09-27 17:59:42

我正试图在C#中找到一个好的正则表达式来实现这一点:

基于文件夹+文件名,我想将文件名复制到一个新名称。

示例:

输入:C:''Test''Sample_Value1''File1.txt
输出:C:''otherFolder''File1_value1.txt

->当我在路径中找到Value1时,我想将文件复制到另一个位置,文件名设置为File1_Value1.text.

输入:C:''Test''Sample_Value2''OtherFile.txt
输出:C:''otherFolder''OtherFile_Value2.txt

->当我在路径中找到Value2时,我想将文件复制到另一个位置,文件名设置为OtherFile_Value2.txt

在我的代码中,"Value1"Value2"可以是静态值。我会有一个"值"列表,这样搜索。

知道吗?

基于文件夹名称复制文件的良好Regex

因此,也许一种简单的方法会起作用:

var dir = Path.GetDirectoryName(input);
var fileName = Path.GetFileNameWithoutExtension(input);
var ext = Path.GetExtension(input);
var output = "";
if (dir.Contains("_Value1"))
{
    output = Path.Combine(@"C:'otherFolder", string.Format("{0}_Value1{2}",
        fileName, ext));
}

同样的代码也适用于Value2。显然,这可以被放入一个扩展方法中,接受几个参数并使其成为动态的。

对于正则表达式,我看不出有什么令人信服的理由。

resultString = Regex.Replace(subjectString, @"^C:''Test''.*?_(.*?)''(.*?)'.(.*?)$", @"C:'otherFolder'$2_$1.$3", RegexOptions.IgnoreCase);

解释:

Assert position at the beginning of the string «^»
Match the character string “C:” literally (case sensitive) «C:»
Match the backslash character «''»
Match the character string “Test” literally (case sensitive) «Test»
Match the backslash character «''»
Match any single character that is NOT a line break character (line feed) «.*?»
   Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “_” literally «_»
Match the regex below and capture its match into backreference number 1 «(.*?)»
   Match any single character that is NOT a line break character (line feed) «.*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the backslash character «''»
Match the regex below and capture its match into backreference number 2 «(.*?)»
   Match any single character that is NOT a line break character (line feed) «.*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “.” literally «'.»
Match the regex below and capture its match into backreference number 3 «(.*?)»
   Match any single character that is NOT a line break character (line feed) «.*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Assert position at the end of the string, or before the line break at the end of the string, if any (line feed) «$»
C:'otherFolder'$2_$1.$3
Insert the character string “C:” literally «C:»
Insert the backslash character «'»
Insert the character string “otherFolder” literally «otherFolder»
Insert the backslash character «'»
Insert the text that was last matched by capturing group number 2 «$2»
Insert the character “_” literally «_»
Insert the text that was last matched by capturing group number 1 «$1»
Insert the character “.” literally «.»
Insert the text that was last matched by capturing group number 3 «$3»
string input = @"C:'this'is'my'myvalue'test.txt";
string lookingfor = "myvalue";
        if (input.Contains(lookingfor))
        {
            //Format new name
           string newdir = @"C:'newdir'" + Path.GetFileName(input) + "_" + lookingfor;
           Trace.WriteLine(newdir);
           //Copy/Move Logic here using File.Copy()
           File.Copy(input, newdir);
        }
        else
        {
            //Do nothing
        }

根据您似乎已经掌握的信息,您不需要regex,但可以通过这种方式解决它。

获取您需要的东西,只需按照下面的方式重新格式化字符串,然后使用File.Copy来移动文件。