我對RegEx非常新穎 - 所以有人可以幫我弄清楚到底什麼地方出了問題?RegEx - 量詞{x,y}沒有任何錯誤
我有這樣的代碼:
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
}
如何定義的模式,如果它被傳遞到這個循環?
具體而言 - 我在代碼中的模式是爲了擺脫文件名中的無效字符。因此我需要擺脫星號,代字號,英鎊符號,括號,尖括號等。 這是正確的模式嗎? – yeahumok 2010-06-28 19:45:15