2

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"

Community
  • 1
  • 1
Rabbott
  • 4,282
  • 1
  • 30
  • 53

1 Answers1

4

To extract 18744594 only

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

To extract twitpic and 18744594

/http:\/\/(twitpic)\.com\/(\d+)/
YOU
  • 120,166
  • 34
  • 186
  • 219
  • and if the ID was alphanumeric, I would just change the d+ to w+ ? – Rabbott Apr 28 '10 at 04:34
  • Oh.. and the reason i am asking to extract twitpic, is for cases when its NOT twitpic.. i want to know which it is.. so without providing twitpic, i need to know what is between http:// and .com - could be tweetphone or yfrog for instance – Rabbott Apr 28 '10 at 04:35
  • 2
    That would be /http:\/\/(\w+)\.com\/(\w+)/ then 'twitpic' would be $1 and 18744594 would be $2. – Michael Ulm Apr 28 '10 at 04:42
  • Thanks Michael, I was late to response. And @Rabbott, yeah `\w+` would be ok for alphanumeric, `\w` is `[a-zA-Z0-9_]` – YOU Apr 28 '10 at 04:49