在rails中调用多个扫描

时间:2015-10-26 21:47:14

标签: ruby-on-rails ruby regex

说我有以下网址:

url = "https://instagram.com/username/"

我想提取"用户名"。可以扫描3次吗?或者这会慢得多吗?

url.scan(/instagram.com\/.+\//).first.scan(/\/.+\//).first.scan(/[^\/]/).first

1 个答案:

答案 0 :(得分:2)

您可以尝试这样的事情:

url = "https://instagram.com/username/somewhat/something/"

url.scan(/https:\/\/instagram.com\/(\w+)\//).first 
# => ["username"]

Demonstration