Grzegorz Kazulak
Grzegorz Kazulak

Reputation: 424

Get the number between specified strings

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

Answers (4)

vrish88
vrish88

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

Ryan Bigg
Ryan Bigg

Reputation: 107728

"http://example.com/news/3226-some-post-title.html".split("/").last.to_i

Upvotes: 1

Gumbo
Gumbo

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

runako
runako

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:

http://www.rubular.com

Upvotes: 1

Related Questions