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!
}
精彩评论