使用RegEx拆分链接

use*_*676 2 .net c# regex url

我想从链接中找到类别.我正在使用像这样的正则表达式将链接分解为数组.

string input = link;
string pattern = "/";            // Split on hyphens 

string[] substrings = Regex.Split(input, pattern);
foreach (string match in substrings)
{
    Console.WriteLine("'{0}'", match);
}
Run Code Online (Sandbox Code Playgroud)

我的链接是:

http://www.example.com/lifestyle/food/a93ypt9-1227277841603?from=public_atom
http://www.example.com/sports/scoccer/accept9-1227277841603?from=public_atom
Run Code Online (Sandbox Code Playgroud)

而上述链接的类别

lifestyle
sports
Run Code Online (Sandbox Code Playgroud)

我正在拆分连字符上的链接并查找我的类别但是有没有更好的方法来完成我的任务?

Jef*_*ado 6

为什么你认为你需要使用正则表达式?本Uri类可以分析和解释你的路径.你可以直接访问Segments.

var uri = new Uri("http://www.link.com/lifestyle/food/a93ypt9-1227277841603?from=public_atom");
uri.Segments; // [ "/", "lifestyle/", "food/", "a93ypt9-1227277841603" ]
Run Code Online (Sandbox Code Playgroud)