在FileName中摆脱多个句点的问题

时间:2010-07-20 16:12:29

标签: c# regex

我正在尝试使用类似的文件名:
MAX_1.01.01.03.pdf看起来像Max_1010103.pdf。

目前我有这段代码:

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

  Regex regExPattern = new Regex(regPattern);
  Regex regExPattern2 = new Regex(@"\s{2,}");
  Regex regExPattern3 = new Regex(@"\.(?=.*\.)");
  string replace = "";

  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);
      sanitizedFileName = regExPattern2.Replace(sanitizedFileName, 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);
          newFileName = regExPattern2.Replace(newFileName, replacement);
          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);
        }

//HERE IS WHERE I AM TRYING TO GET RID OF DOUBLE PERIODS//
        if (regExPattern3.IsMatch(files2))
        {
          string filewithDoublePName = System.IO.Path.GetFileName(files2);
          string doublepPath = System.IO.Path.GetDirectoryName(files2);
          string name = System.IO.Path.GetFileNameWithoutExtension(files2);
          string newName = name.Replace(".", "");
          string filesDir = System.IO.Path.GetDirectoryName(files2);
          string fileExt = System.IO.Path.GetExtension(files2);
          string newPath = System.IO.Path.Combine(filesDir, newName+fileExt);

          DataGridViewRow clean = new DataGridViewRow();
          clean.CreateCells(dataGridView1);
          clean.Cells[0].Value =doublepPath;
          clean.Cells[1].Value = filewithDoublePName;
          clean.Cells[2].Value = newName;
          dataGridView1.Rows.Add(clean);
        }
      }
    }
    catch (Exception e)
    {
      throw;
      //errors.Write(e);
    }
  }

我跑了这个而不是摆脱所有期间(减去文件扩展名之前的时间段),我得到的结果如下:MAX_1.0103.pdf

如果有多个句点,例如:Test....1.txt,我会得到以下结果:Test...1.txt

似乎只能摆脱一个时期。我是正则表达式的新手,这是该项目的要求。谁能帮助我弄清楚我在这里做错了什么?

谢谢!

已编辑以显示代码中所做的更改

5 个答案:

答案 0 :(得分:12)

为什么不使用Path class

string name = Path.GetFileNameWithoutExtension(yourPath);
string newName = name.Replace(".", "");
string newPath = Path.Combine(Path.GetDirectoryName(yourPath),
                              newName + Path.GetExtension(yourPath));

为清晰起见,每个步骤分开。

所以输入

  

“C:\用户\佛瑞德\ MAX_1.01.01.03.pdf”

我得到了输出

  

“C:\用户\佛瑞德\ MAX_1010103.pdf”

这是我所期待的。

如果我提供:

  

“C:\用户\ Fred.Flintstone \ MAX_1.01.01.03.pdf”

我明白了:

  

“C:\用户\ Fred.Flintstone \ MAX_1010103.pdf”

再一次我的期望,因为我没有处理路径的“DirectoryName”部分。

注意我忽略了RegEx是一项要求。仍然坚持这个答案。

答案 1 :(得分:2)

说,你不是already ask this question吗?

无论如何,我坚持my original answer

string RemovePeriodsFromFilename(string fullPath)
{
    string dir = Path.GetDirectoryName(fullPath);
    string filename = Path.GetFileNameWithoutExtension(fullPath);
    string sanitized = filename.Replace(".", string.Empty);
    string ext = Path.GetExtension(fullPath);

    return Path.Combine(dir, sanitized + ext);
}

现在,既然你指定必须使用RegEx,我想你总是强制在那里:

string RemovePeriodsFromFilename(string fullPath)
{
    string dir = Path.GetDirectoryName(fullPath);
    string filename = Path.GetFileNameWithoutExtension(fullPath);

    // Look! Now the solution uses RegEx!
    string sanitized = Regex.Replace(filename, @"\.", string.Empty);

    string ext = Path.GetExtension(fullPath);

    return Path.Combine(dir, sanitized + ext);
}

注意:这基本上与ChrisF建议的方法完全相同。

无论谁要求您使用RegEx,我建议您请求解释原因。

答案 2 :(得分:0)

我一起放弃了正则表达式,这样做:

  1. 将所有句点替换为空 字符串
  2. 替换最后3个 (“。”+最后3个字符) 字符)

答案 3 :(得分:0)

此正则表达式将删除除3或4个字母扩展名之前的所有期间。

string filename = "test.test......t.test.pdf";    
string newFilename = new Regex(@"\.(?!(\w{3,4}$))").Replace(filename, "");

如果您希望它使用2个字母的扩展名,只需将{3,4}更改为{2,4}

即可 祝你好运!

答案 4 :(得分:-1)

像这样的东西,也许:

string fileName = "MAX_1.01.01.03.pdf";
fileName = fileName.Substring(0, 1).ToUpper() + fileName.Substring(1).ToLower();
fileName = fileName.Replace(".", "");