【问题标题】:How to Combine Regex Lines into Single Regex to Remove Space and . in C#如何将正则表达式行组合成单个正则表达式以删除空格和 .在 C# 中
【发布时间】:2011-07-10 03:14:46
【问题描述】:

如何将以下正则表达式合并为一个以删除文件中的所有空格和句点?我想出了如何替换单个字符,而不是一行中的多个字符。

        string testfile = @"C:\test\work\A Nightmare.On.Elm Street 1984.720p.BRRip.nfo";
        testfile = testfile.Substring(0, testfile.Length - 4);
        testfile = Regex.Replace(testfile, @"\.", "");
        testfile = Regex.Replace(testfile, @"\s", "");

谢谢!

【问题讨论】:

  • testfile = testfile.Replace(".", "")

标签: c# regex


【解决方案1】:

要使用的正则表达式是@"[\s\.]"

testfile = Regex.Replace(testfile, @"[\s\.]", "");

【讨论】:

  • 点在[] 中没有特殊含义。不需要额外的斜线。
【解决方案2】:

试试这个:

testfile = Regex.Replace(testfile, @"[.\s]", "");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-10
    • 2016-06-17
    • 2018-06-11
    • 1970-01-01
    • 2011-11-01
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多