我有以下一组Urls:
http://test/mediacenter/Photo Gallery/Conf 1/1.jpg
http://test/mediacenter/Photo Gallery/Conf 2/3.jpg
http://test/mediacenter/Photo Gallery/Conf 3/Conf 4/1.jpg我要做的就是从urls中提取出Conf 1,Conf 2,Conf 3,在“照片库”之后的级别(Urls不是静态的,它们共享公共级别,即照片库)
任何帮助都是非常感谢的。
发布于 2014-03-17 16:13:05
不需要regex
string testCase = "http://test/mediacenter/Photo Gallery/Conf 1/1.jpg";
string urlBase = "http://test/mediacenter/Photo Gallery/";
if(!testCase.StartsWith(urlBase))
{
throw new Exception("URL supplied doesn't belong to base URL.");
}
Uri uriTestCase = new Uri(testCase);
Uri uriBase = new Uri(urlBase);
if(uriTestCase.Segments.Length > uriBase.Segments.Length)
{
System.Console.Out.WriteLine(uriTestCase.Segments[uriBase.Segments.Length]);
}
else
{
Console.Out.WriteLine("No child segment...");
}发布于 2014-03-17 16:12:46
有必要使用Regex吗?你不需要像这样使用Regex就能得到它
string str= @"http://test/mediacenter/Photo Gallery/Conf 1/1.jpg";
var z=qq.Split('/')[5];或
var x= new Uri(str).Segments[3];发布于 2014-03-17 16:16:35
这应该会使你:
var s = @"http://test/mediacenter/Photo Gallery/Conf 11/1.jpg";
var regex = new Regex(@"(Conf \d*)");
var match = regex.Match(s);
Console.WriteLine(match.Groups[0].Value); // Prints a当然,您必须确信'Conf‘(其中x是一个数字)不会出现在URL的其他地方。
这将通过去掉示例中的多个文件夹(Conf 3/Conf 4)来稍微改进它。
var regex = new Regex(@"((Conf \d*/*)+)");不过,它留下了尾随的/。
https://stackoverflow.com/questions/22458968
复制相似问题