2010-07-09 180 views
2

我有一个应用程序,用空格替换“无效”字符(由我的正则表达式定义)。我想要它,以便如果文件名中有两个或更多个空格,请修剪一个。例如:正则表达式 - 摆脱双空白?

Deal A & B.txt我的应用程序运行后,将被重命名为Deal A   B.txt(3个空间B/W A和B)。我想要的是这样的:Deal A B.txt(A和B之间的一个空格)。

我想确定如何做到这一点 - 我想我的应用程序将不得不通过所有文件名运行至少一次以替换无效字符,然后再次运行文件名以摆脱无关的空格。

有人可以帮我吗?
这是目前用于替换无效字符我的代码:

public partial class CleanNames : Form 
{ 
    public CleanNames() 
    { 
     InitializeComponent(); 

    } 

    public void Sanitizer(List<string> paths) 
    { 
     string regPattern = (@"[~#&$!%+{}]+"); 
     string replacement = " "; 

     Regex regExPattern = new Regex(regPattern); 


     StreamWriter errors = new StreamWriter(@"S:\Testing\Errors.txt", true); 
     var filesCount = new Dictionary<string, int>(); 


     dataGridView1.Rows.Clear(); 

      try 
      { 

       foreach (string files2 in paths) 
       { 

       string filenameOnly = System.IO.Path.GetFileName(files2); 
       string pathOnly = System.IO.Path.GetDirectoryName(files2); 
       string sanitizedFileName = regExPattern.Replace(filenameOnly, replacement); 
       string sanitized = System.IO.Path.Combine(pathOnly, sanitizedFileName); 


       if (!System.IO.File.Exists(sanitized)) 
       { 
        DataGridViewRow clean = new DataGridViewRow(); 
        clean.CreateCells(dataGridView1); 
        clean.Cells[0].Value = pathOnly; 
        clean.Cells[1].Value = filenameOnly; 
        clean.Cells[2].Value = sanitizedFileName; 
        dataGridView1.Rows.Add(clean); 

        System.IO.File.Move(files2, sanitized); 
       } 

       else 
       { 
        if (filesCount.ContainsKey(sanitized)) 
        { 
         filesCount[sanitized]++; 
        } 
        else 
        { 
         filesCount.Add(sanitized, 1); 
        } 
        string newFileName = String.Format("{0}{1}{2}", 
        System.IO.Path.GetFileNameWithoutExtension(sanitized), 
        filesCount[sanitized].ToString(), 
        System.IO.Path.GetExtension(sanitized)); 
        string newFilePath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(sanitized), newFileName); 
        System.IO.File.Move(files2, newFilePath); 
        sanitized = newFileName; 

        DataGridViewRow clean = new DataGridViewRow(); 
        clean.CreateCells(dataGridView1); 
        clean.Cells[0].Value = pathOnly; 
        clean.Cells[1].Value = filenameOnly; 
        clean.Cells[2].Value = newFileName; 

        dataGridView1.Rows.Add(clean); 

       } 




       } 
      } 
      catch (Exception e) 
      { 
       errors.Write(e); 
      } 


    } 

    private void SanitizeFileNames_Load(object sender, EventArgs e) 
    { } 

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 
    { 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     Application.Exit(); 
    } 


} 

的问题是,一个重命名后,不是所有的文件将具有blankspaces相同数量。如在,我可以有Deal A&B.txt其中重命名后将成为Deal A B.txt(1空间B /瓦A和B - 这很好)。但我也将有如下文件:Deal A & B & C.txt重命名后:Deal A   B   C.txt(A,B和C之间3个空格 - 不可接受)。

有没有人有任何想法/代码如何做到这一点?

回答

2

这有帮助吗?

 var regex = new System.Text.RegularExpressions.Regex("\\s{2,}"); 
     var result = regex.Replace("Some text with a lot  of spaces, and 2\t\ttabs.", " "); 
     Console.WriteLine(result); 

输出是:

Some text with a lot of spaces, and 2 tabs. 

它只是取代的2个或更多空白字符与单个空间中的任何序列...


编辑:

为了澄清,我只是在你现有的正则表达式之后执行这个正则表达式:

public void Sanitizer(List<string> paths) 
{ 
    string regPattern = (@"[~#&$!%+{}]+"); 
    string replacement = " "; 

    Regex regExPattern = new Regex(regPattern); 
    Regex regExPattern2 = new Regex(@"\s{2,}"); 

和:

  foreach (string files2 in paths) 
      { 

      string filenameOnly = System.IO.Path.GetFileName(files2); 
      string pathOnly = System.IO.Path.GetDirectoryName(files2); 
      string sanitizedFileName = regExPattern.Replace(filenameOnly, replacement); 
      sanitizedFileName = regExPattern2.Replace(sanitizedFileName, replacement); // clean up whitespace 
      string sanitized = System.IO.Path.Combine(pathOnly, sanitizedFileName); 

我希望,更有意义。

+0

这是否需要一个新的foreach循环后,我完成了“消毒”文件? – yeahumok 2010-07-09 15:06:32

+0

@yeahumok请参阅我上面的修改。如果你现有的循环,只需在第一个之后添加第二个正则表达式。 – CodingWithSpike 2010-07-09 15:16:04

+0

非常感谢你!这工作,它完全有道理:)我感谢您的帮助! – yeahumok 2010-07-09 15:33:02

1

完成清理之后,只需用2个空格替换2个空格,而字符串中存在2个空格。

while (mystring.Contains(" ")) mystring = mystring.Replace(" "," "); 

我认为这是正确的语法...

5

做的等值当地货币:

s/\s+/ /g; 
+0

+1我喜欢这个比字符串替换解决方案好一点。如果你已经在使用Regex的话,不妨继续做下去。这具有清除所有空白(并且在一次通过中)而不是仅仅打空间的优点。在实践中,我怀疑可读性或性能会受到任何解决方案的影响,我怀疑他的文本实际上除了空格之外还会有其他任何空格。但是,这仍然是明智的。 – Brian 2010-07-09 15:23:11

1

可以执行另一个正则表达式替换后的第一个

@" +" -> " "

1

正如Fosco所说,格式化为:

while (mystring.Contains(" ")) mystring = mystring.Replace(" "," "); 

//      ||         || | 
+0

我会在哪里添加此声明?我需要另一个foreach循环吗? – yeahumok 2010-07-09 15:05:12

+0

你可以在设置'sanitizedFileName = regExPattern.Replace(filenameOnly,replacement);'并用它来代替'sanitizedFileName'后添加此语句。当然,还有其他地方可以放,但我认为这是最好的选择。 – Brian 2010-07-09 15:25:32

4

只需在您的regPattern中添加一个空格即可。任何无效字符和空格的集合都将被替换为一个空格。您可能会浪费一点时间来替换空间,但另一方面,您不需要第二个字符串操作调用。

+0

+1:其他一些解决方案使用*多于两个循环。当你可以在一个循环中完成整个工作时,为什么要麻烦呢? – 2010-07-09 15:40:07

+0

这对我有意义 - 你的正则表达式就像's/[&* ^] +// g',用空格替换任何_series_无效字符(包括空格)。 – 2010-07-09 15:40:09

+0

我放弃了这个想法,因为它会错过像'na!me $' – ULysses 2010-07-09 15:42:29