开发者

Regex to get article ID from url

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜