基于文件夹名称的复制文件的良好正则表达式

时间:2014-04-23 14:23:58

标签: c# regex

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

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

示例:

  

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

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

  

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

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

" Value1" "值2"可以是我的代码中的静态值。我将列出一个'值'像这样搜索。

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

所以也许一个简单的方法可行:

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。显然,这可以放入扩展方法并接受一些参数并使其动态化。

我无法在正则表达式中看到任何令人信服的理由。

答案 1 :(得分:1)

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»

答案 2 :(得分:0)

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
        }

根据您似乎已经拥有的信息,您不需要正则表达式,但您可以通过这种方式解决它。

抓住你需要的东西,然后自己重​​新格式化字符串,然后使用File.Copy移动文件。