开发者

Regex to get article ID from url

开发者 https://www.devze.com 2022-12-25 16:33 出处:网络
My url looks like: www.example.com/a-292-some-text-here// 292 www.example.com/a-2-asdfss// 2 www.example.com/a-44-333// 44

My url looks like:

www.example.com/a-292-some-text-here  // 292
www.example.com/a-2-asdfss       // 2
www.example.com/a-44-333      // 44

I need a r开发者_运维知识库egex to get the 292 from the url.

The ID will always be an integer.

i am using C#.


Use Regex.Match with @"\d+":

string input = "www.example.com/a-292-some-text-here";
Match match = Regex.Match(input, @"\d+");
if (match.Success)
{
    int id = int.Parse(match.Value);
    // Use the id...
}
else
{
    // Error!
}
0

精彩评论

暂无评论...
验证码 换一张
取 消