正则表达式无法在数组中正常工作

时间:2018-08-20 15:32:44

标签: c# regex

我希望能够搜索文件夹和子文件夹的名称,然后如果它们的名称上有特定的样式,请更改或从文件夹名称中删除它们。 我使用了以下代码,但folderNameList返回了{}

string[] dirs = Directory.GetDirectories(myRootPath, "*", SearchOption.AllDirectories);


string findFolderNamePattern = @"((?i)mydomain.com|sitename(?-i))";
string replacement = " ";
Match folderNameList = Regex.Match(dirs.ToString(), findFolderNamePattern);

                foreach (var folder in folderNameList.ToString())
                {
                    folder = Regex.Replace(folder.ToString(), replacement);
                }

您还可以看到正则表达式this

1 个答案:

答案 0 :(得分:1)

您需要做的就是这个

string[] directories = Directory.GetDirectories(myRootPath, "*", SearchOption.AllDirectories);

string findFolderNamePattern = @"((?i)mydomain.com|sitename(?-i))";
string replacement = " ";            

foreach (var directory in directories)
{
    var newFolder = Regex.Replace(directory, findFolderNamePattern, replacement);
}