正则表达式忽略C#中的模式

时间:2014-06-26 17:18:12

标签: c# regex

enter image description here

我将删除所有匹配结果(蓝色区域)表单文件名以删除额外的字符串,因此我想删除“Part”和“”并将数字保留在红色区域中,如何实现该模式? / p>

3 个答案:

答案 0 :(得分:2)

使用以下正则表达式:

-Part-\d+|^Part\s+|(?<=^Part\s+\d+)\s+

Regular expression visualization

示例代码:

string[] input = {
    "102Redirect-http-to-https-in-iis-Part-102.mp4",
    "4Events-in-the-life-cycle-of-a-web-application-Part-4.mp4",
    "Part 5   Menu control in asp net.mp4",
    "Part 153   Menu control in asp net.mp4"
};
string[] output = input.Select(s => Regex.Replace(s, @"-Part-\d+|^Part\s+|(?<=^Part\s+\d+)\s+", string.Empty))
                       .ToArray();
foreach (string s in output)
    Console.WriteLine(s);

输出:

102Redirect-http-to-https-in-iis.mp4
4Events-in-the-life-cycle-of-a-web-application.mp4
5Menu control in asp net.mp4
153Menu control in asp net.mp4

答案 1 :(得分:1)

不要使用正则表达式,只需替换:

String fileName=fileName.replace("-","");
fileName=fileName.replace("Part","");
fileName=fileName.replace(new Regex("   ",""); // or you can use \t of regex which take tabulations.

答案 2 :(得分:0)

这将删除“Part”以及空格和数字,并将其限制为仅出现在行的开头。

^部分\ S \ d + \ S {3}