Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 路径中最后一个文件夹的正则表达式_Regex_C# 4.0 - Fatal编程技术网

Regex 路径中最后一个文件夹的正则表达式

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\

我一直在尝试用C语言中的正则表达式捕获文件夹路径中的最后一个文件夹,但我对这个太陌生了,无法理解这一点。例如,如果我有C:\Projects\Test,那么表达式应该返回Test。如果我有H:\Programs\Somefolder\Someotherfolder\Final,那么结果应该是Final。我试过下面的代码,但它只是爆炸了。谢谢你的帮助

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);