2009-04-16 45 views

回答

0

嘗試這種模式:

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

所以:

match = /http:\/\/example\.com\/news\/(\d+)-.+\.html/.match("http://example.com/news/3226-some-post-title.html") 
puts match[1] 
2

你可以使用:

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

這將在「/」後搶位(\ d),並把數字到第一變量,一旦它發現了它們。

一個偉大的地方,以測試正則表達式是http://rubular.com/

+0

你需要逃脫最後。 – 2009-04-17 00:13:54

1

你想這樣的:

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

\ d是任何數字。此外,以下網站在紅寶石的正則表達式非常有用:

http://www.rubular.com

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