Jackson Cunningham
Jackson Cunningham

Reputation: 5073

Calling multiple scans in rails

Say I have the following url:

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

And I want to extract "username". Is it okay to scan 3 times? Or is this going to be much slower?

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

Upvotes: 1

Views: 37

Answers (1)

potashin
potashin

Reputation: 44581

You can try something like this:

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

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

Demonstration

Upvotes: 2

Related Questions