c# - 正则表达式——去掉双空格?

标签 c# regex

我有一个应用程序,它用空格替换“无效”字符(由我的正则表达式定义)。我想要它,以便如果文件名中有 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();
    }


}

问题是,并非所有重命名后的文件都具有相同数量的空格。比如,我可以有 Deal A&B.txt,重命名后会变成 Deal A B.txt(1 个空格 b/w A 和 B——这没问题) .但我也会有这样的文件:Deal A & B & C.txt 重命名后是:Deal A B C.txt(A 之间有 3 个空格, B 和 C—— Not Acceptable )。

有没有人有任何想法/代码来完成这个?

最佳答案

做本地等效的:

s/\s+/ /g;

关于c# - 正则表达式——去掉双空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3213764/

相关文章:

c# - 单元测试自定义 FxCop 规则

c# - 如何使用正则表达式更改字符串中的组

c# - 更新大型SQLite数据库

匹配keyworks的python过滤行

regex - 获取括号内的多个值

regex - 递增 Python 字符串中的最后一位数字

c# - 使用 if 语句将组合框值与两个文本框值进行比较

c# - 什么是多态?

Java 正则表达式 : match whole word with word boundary

ruby - 如何只匹配某些字符与正则表达式?