Search code examples
ruby-on-railsregextwitpic

Rails regex to extract Twitpic link/code if present


Provided a url, within a string of text (tweet) such as

"Check out my twitpic http://twitpic.com/1876544594 its awesome"

I need a Rails regex that will return 18744594, the id of this particular twitpic...

This question has been asked here but was for PHP, and I need it for Rails.

I would also be able to pull the name of the site, so the text between http:// and .com "twitpic"


Solution

  • To extract 18744594 only

    /http:\/\/twitpic\.com\/(\d+)/
    

    To extract twitpic and 18744594

    /http:\/\/(twitpic)\.com\/(\d+)/