views:

98

answers:

4

Ok. Given the example of:

http://example.com/news/3226-some-post-title.html

I'm trying to get the 3226. This regexp: http:\/\/interaktywnie.com\/newsy\/(.*).html doesn't seem to work. Please help.

Thanks.

+2  A: 

You can just use:

   /\/(\d+)-(.*)\.html$/

This will grab the digits (\d) after the '/' and put the digits into the first variable once it finds them.

A great place to test regular expression is http://rubular.com/.

vrish88
you need to escape that last .
Chas. Owens
ah thanks. good eye.
vrish88
+1  A: 

You want this:

/http:\/\/example.com\/news\/(\d+)-.+\.html/

\d is any digit. Also, the following site is very useful for regular expressions in ruby:

http://www.rubular.com

runako
A: 

Try this pattern:

/http:\/\/example\.com\/news\/(\d+)-.+\.html/

So:

match = /http:\/\/example\.com\/news\/(\d+)-.+\.html/.match("http://example.com/news/3226-some-post-title.html")
puts match[1]
Gumbo
+1  A: 
"http://example.com/news/3226-some-post-title.html".split("/").last.to_i
Ryan Bigg