views:

161

answers:

1

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"

+4  A: 

To extract 18744594 only

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

To extract twitpic and 18744594

/http:\/\/(twitpic)\.com\/(\d+)/
S.Mark
and if the ID was alphanumeric, I would just change the d+ to w+ ?
Rabbott
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
That would be /http:\/\/(\w+)\.com\/(\w+)/ then 'twitpic' would be $1 and 18744594 would be $2.
Michael Ulm
Thanks Michael, I was late to response. And @Rabbott, yeah `\w+` would be ok for alphanumeric, `\w` is `[a-zA-Z0-9_]`
S.Mark