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?
if the tweet ID is the last part of the url, you can use:
the $ means the end of the string