Regex 路径中最后一个文件夹的正则表达式
我一直在尝试用C语言中的正则表达式捕获文件夹路径中的最后一个文件夹,但我对这个太陌生了,无法理解这一点。例如,如果我有C:\Projects\Test,那么表达式应该返回Test。如果我有H:\Programs\Somefolder\Someotherfolder\Final,那么结果应该是Final。我试过下面的代码,但它只是爆炸了。谢谢你的帮助Regex 路径中最后一个文件夹的正则表达式,regex,c#-4.0,Regex,C# 4.0,我一直在尝试用C语言中的正则表达式捕获文件夹路径中的最后一个文件夹,但我对这个太陌生了,无法理解这一点。例如,如果我有C:\Projects\Test,那么表达式应该返回Test。如果我有H:\Programs\Somefolder\Someotherfolder\Final,那么结果应该是Final。我试过下面的代码,但它只是爆炸了。谢谢你的帮助 string pattern = ".*\\([^\\]+$)"; Match match = Regex.Match("H:\\Projects\
string pattern = ".*\\([^\\]+$)";
Match match = Regex.Match("H:\\Projects\\Final", pattern, RegexOptions.IgnoreCase);
为什么不使用split
字符串str=c:\temp\temp1\temp2
字符串lastfolder=str.Split\.Last 为什么不使用split
字符串str=c:\temp\temp1\temp2
字符串lastfolder=str.Split\.Last 也许是这个
string strRegex = @".*\\(.*)"; RegexOptions myRegexOptions = RegexOptions.IgnoreCase | RegexOptions.Multiline;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = @"H:\Programs\Somefolder\Someotherfolder\Final";
string strReplace = @"$1";
return myRegex.Replace(strTargetString, strReplace);
也许是这个
string strRegex = @".*\\(.*)"; RegexOptions myRegexOptions = RegexOptions.IgnoreCase | RegexOptions.Multiline;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = @"H:\Programs\Somefolder\Someotherfolder\Final";
string strReplace = @"$1";
return myRegex.Replace(strTargetString, strReplace);
你为什么用正则表达式。你可以用
你为什么用正则表达式。你可以用
如果您有一套相当完整的.NET库可以为您执行此操作,那么使用正则表达式是不好的。。。下面是使用System.IO.Path或System.IO.DirectoryInfo的两种简单方法
string path = @"H:\Programs\Somefolder\Someotherfolder\Final";
Console.WriteLine(System.IO.Path.GetFileName(path));
Console.WriteLine(new System.IO.DirectoryInfo(path).Name);
如果您有一套相当完整的.NET库可以为您执行此操作,那么使用正则表达式是不好的。。。下面是使用System.IO.Path或System.IO.DirectoryInfo的两种简单方法
string path = @"H:\Programs\Somefolder\Someotherfolder\Final";
Console.WriteLine(System.IO.Path.GetFileName(path));
Console.WriteLine(new System.IO.DirectoryInfo(path).Name);