【发布时间】:2010-06-28 19:40:43
【问题描述】:
我是 RegEx 的新手——所以有人可以帮我弄清楚这里到底出了什么问题吗?
我有这个代码:
string regPattern = "*[~#%&*{}/<>?|\"-]+*";
string replacement = "";
Regex regExPattern = new Regex(regPattern);
然而,当我的应用程序遇到 regExPattern 行时,我得到一个 ArgumentException -- 量词 {x,y} 没有任何错误。
有人可以帮忙吗?
编辑:我需要像这样将这个模式传递给一个 foreach 循环:
if (paths.Contains(regPattern))
{
foreach (string files2 in paths)
{
try
{
string filenameOnly = Path.GetFileName(files2);
string pathOnly = Path.GetDirectoryName(files2);
string sanitizedFileName = regExPattern.Replace(filenameOnly, replacement);
string sanitized = Path.Combine(pathOnly, sanitizedFileName);
//write to streamwriter
System.IO.File.Move(files2, sanitized);
}
catch (Exception ex)
{
//write to streamwriter
}
}
}
else
{
//write to streamwriter
}
如果模式被传递到这个循环中,我如何定义它?
【问题讨论】:
-
具体来说——我在代码中使用的模式旨在消除文件名中的那些无效字符。因此我需要去掉星号、波浪号、井号、括号、尖括号等。这是正确的模式吗?
标签: c# regex argumentexception