给定路径和某个部分,如何找到该部分正下方的文件夹名称?
这很难解释,让我举一些例子。假设我在'Dev / Branches'下面找到文件夹的名称。以下是示例输入,预期结果以粗体显示
我正在使用C#
编辑:我想我可以使用正则表达式/Dev/Branches/(.*?)/
捕获第一组,但有没有正则表达式的整洁解决方案?无论如何,正则表达式会在第二种情况下失败。
答案 0 :(得分:1)
// starting path
string path = @"C:\Code\Dev\Branches\Latest\bin\abc.dll";
// search path
string search = @"Dev\Branches";
// find the index of the search criteria
int idx = path.IndexOf(search);
// determine whether to exit or not
if (idx == -1 || idx + search.Length >= path.Length) return;
// get the substring AFTER the search criteria, split it and take the first item
string found = path.Substring(idx + search.Length).Split("\\".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).First();
Console.WriteLine(found);
答案 1 :(得分:1)
以下代码将完全符合您的预期:
public static string GetSubdirectoryFromPath(string path, string parentDirectory, bool ignoreCase = true)
{
// 1. Standarize the path separators.
string safePath = path.Replace("/", @"\");
string safeParentDirectory = parentDirectory.Replace("/", @"\").TrimEnd('\\');
// 2. Prepare parentDirectory to use in Regex.
string directory = Regex.Escape(safeParentDirectory);
// 3. Find the immediate subdirectory to parentDirectory.
Regex match = new Regex(@"(?:|.+)" + directory + @"\\([^\\]+)(?:|.+)", ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None);
// 4. Return the match. If not found, it returns null.
string subDirectory = match.Match(safePath).Groups[1].ToString();
return subDirectory == "" ? null : subDirectory;
}
测试代码:
void Test()
{
string path1 = @"C:\Code\Dev\Branches\Latest\bin\abc.dll";
string path2 = @"C:\Dev\Branches\5.1";
string path3 = @"D:\My Documents\Branches\7.0\Source\test.cs";
Console.WriteLine("Matches:");
Console.WriteLine(GetSubdirectoryFromPath(path1, "dev/branches/") ?? "Not found");
Console.WriteLine(GetSubdirectoryFromPath(path1, @"Dev\Branches") ?? "Not found");
Console.WriteLine(GetSubdirectoryFromPath(path3, "D:/My Documents/Branches") ?? "Not found");
// Incorrect parent directory.
Console.WriteLine(GetSubdirectoryFromPath(path2, "My Documents") ?? "Not found");
// Case sensitive checks.
Console.WriteLine(GetSubdirectoryFromPath(path3, @"My Documents\Branches", false) ?? "Not found");
Console.WriteLine(GetSubdirectoryFromPath(path3, @"my Documents\Branches", false) ?? "Not found");
// Output:
//
// Matches:
// Latest
// Latest
// 7.0
// Not found
// 7.0
// Not found
}
答案 2 :(得分:0)
将其分解为更小的步骤,您可以自己解决:
Path.GetDirectoryName(string)
Directory.GetParent(string)
。这归结为;
var directory = Path.GetDirectoryName(input);
var parentDirectory = Directory.GetParent(directory);
提供的C:\Dev\Branches\5.1
- > 5.1
不符合您的规范,即输入路径本身的目录名称。这将输出Branches
。
答案 3 :(得分:0)
new Regex("\\?" + PathToMatchEscaped + "\\(\w+)\\?").Match()...
答案 4 :(得分:0)
我选择了这个
public static string GetBranchName(string path, string prefix)
{
string folder = Path.GetDirectoryName(path);
// Walk up the path until it ends with Dev\Branches
while (!String.IsNullOrEmpty(folder) && folder.Contains(prefix))
{
string parent = Path.GetDirectoryName(folder);
if (parent != null && parent.EndsWith(prefix))
return Path.GetFileName(folder);
folder = parent;
}
return null;
}