如何从该字符串中提取特定位置的文本?

时间:2011-05-08 13:17:08

标签: ruby-on-rails ruby regex

如何在Ruby 1.9.2中提取以下字符串的username位置中的任何文本?

https://www.facebook.com/username/posts/101502204713520282

1 个答案:

答案 0 :(得分:4)

拆分一个或多个/的实例并取第三部分?

ruby-1.9.2-p0 :001 > string = "https://www.facebook.com/username/posts/101502204713520282"
 => "https://www.facebook.com/username/posts/101502204713520282" 
ruby-1.9.2-p0 :002 > string.split(/\/+/)[2]
 => "username"