Reputation: 424
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.
Upvotes: 0
Views: 154
Reputation: 21437
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/.
Upvotes: 2
Reputation: 107728
"http://example.com/news/3226-some-post-title.html".split("/").last.to_i
Upvotes: 1
Reputation: 655239
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]
Upvotes: 0
Reputation: 6152
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:
Upvotes: 1