Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter status URL regex

Tags:

regex

I have an existing regex:

/^http:\/\/twitter\.com\/(\w+)\/status(es)*\/(\d+)$/

that I use for determining if a URL is a twitter status update URL. Eg.

http://twitter.com/allWPthemes/status/2040410213974016

But ever since "new" twitter came out, they have changed the status URL's to look like :

http://twitter.com/#!/allWPthemes/status/2040410213974016

with the added /#!

So my question is : How can I modify my regex to match both URL's?

My final failed attempt was:

^http:\/\/twitter\.com\/(#!\/w+|\w+)\/status(es)*\/(\d+)$
like image 640
vinnie Avatar asked Nov 09 '10 20:11

vinnie


3 Answers

Try this: /^https?:\/\/twitter\.com\/(?:#!\/)?(\w+)\/status(es)?\/(\d+)$/

This will match both the original URLs and the new hash tag URLs.

If you just want to match the new URLs, this should do it: /^https?:\/\/twitter\.com\/#!\/(\w+)\/status(es)?\/(\d+)$/

like image 117
Kevin Avatar answered Oct 14 '22 00:10

Kevin


approved answer will not match shared twitter URLs like this: https://twitter.com/USATODAY/status/982270433385824260?s=19 because end of string flag "$"

// working solution
/^https?:\/\/twitter\.com\/(?:#!\/)?(\w+)\/status(es)?\/(\d+)/

test: https://regex101.com/r/mNsp3o/4

like image 44
webolizzer Avatar answered Oct 13 '22 23:10

webolizzer


Your solution is pretty close. You can simply add the #!/ as an optional element like this:

(#!\/)?

So the full regex would look like this:

/^http:\/\/twitter\.com\/(#!\/)?(\w+)\/status(es)*\/(\d+)$/
like image 43
Gavin Miller Avatar answered Oct 14 '22 01:10

Gavin Miller