ben ben - 3 months ago 27
Ruby Question

How can I fix this regex which extracts a tweet id from a Twitter URL?

I am trying to write a regex that will extract a tweet id from a Twitter URL.

I have this one, which works when the Twitter username has a number in it:

'.*?\\d+.*?(\\d+)'

ruby-1.9.2-p0 > Regexp.new('.*?\\d+.*?(\\d+)',Regexp::IGNORECASE).match('https://twitter.com/#!/sportsguy33/status/41257488166686720')[1]
=> "41257488166686720"
ruby-1.9.2-p0 > Regexp.new('.*?\\d+.*?(\\d+)',Regexp::IGNORECASE).match('http://twitter.com/#!/dailythunder/status/41382006113841153')[1]
=> "3"


And this one, which works when the Twitter username doesn't have a number in it

'.*?(\\d+)'

ruby-1.9.2-p0 > Regexp.new('.*?(\\d+)',Regexp::IGNORECASE).match('https://twitter.com/#!/sportsguy33/status/41257488166686720')[1]
=> "33"
ruby-1.9.2-p0 > Regexp.new('.*?(\\d+)',Regexp::IGNORECASE).match('http://twitter.com/#!/dailythunder/status/41382006113841153')[1]
=> "41382006113841153"


How can I write one that will work in either case?

Answer

if the tweet ID is the last part of the url, you can use:

'\/(\d+)$'

the $ means the end of the string