我的网址是这样的:正则表达式来从URL获取文章ID
www.example.com/a-292-some-text-here // 292
www.example.com/a-2-asdfss // 2
www.example.com/a-44-333 // 44
我需要一个正则表达式来从URL得到292。
的ID将总是整数。
我使用C#。
我的网址是这样的:正则表达式来从URL获取文章ID
www.example.com/a-292-some-text-here // 292
www.example.com/a-2-asdfss // 2
www.example.com/a-44-333 // 44
我需要一个正则表达式来从URL得到292。
的ID将总是整数。
我使用C#。
使用Regex.Match
与@"\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!
}
感谢...更新了一些更多的例子。 – Blankman 2010-04-06 21:10:34
@Blankman:我相信我上面提供的代码给你想要的东西为您的每个实例输入。 – 2010-04-07 05:29:49
会不会有永远是一个','在它的前面?你能举出更多可能的URL的例子吗? – 2010-04-06 21:07:34