Reputation: 16536
I'm looking for an universal regular expression which extracts the twitter username from an url.
Sample URLS
http://www.twitter.com/#!/donttrythis
http://twitter.com/KimKardashian
Upvotes: 15
Views: 11786
Reputation: 339
this regex works fine in jQuery
$('#inputTwitter').blur(function() {
var twitterUserName = $(this).val();
$(this).val(twitterUserName.match(/https?:\/\/(www\.)?twitter\.com\/(#!\/)?@?([^\/]*)/)[3])
});
Upvotes: 7
Reputation: 258
I found Lombo's answer to work the best except it would not work if the URL was www.twitter.com/example . The following works for me on www as well.
$dirty_twitter = array( 'https://twitter.com/', 'http://twitter.com/', 'www.twitter.com/', 'https://www.twitter.com/', 'http://www.twitter.com/', 'twitter.com/' );
$clean_twitter = str_replace( $dirty_twitter, '', $clean_twitter );
Upvotes: 0
Reputation: 6362
This one is based on Lombo's answer, works without http(s) too, is less hungry (not keeping spaces after the username) and returns first in the result.
Check it in action: https://regex101.com/r/xI2vF3/3
For js:
(?:https?:\/\/)?(?:www\.)?twitter\.com\/(?:#!\/)?@?([^\/\?\s]*)
Upvotes: 4
Reputation: 1791
Lombo's answer is my favorite, but it will glom any query string in with the result:
http://www.twitter.com/#!/donttrythis?source=internet
will result in a username of "donttrythis?source=internet"
I'd modify it to be:
preg_match("|https?://(www\.)?twitter\.com/(#!/)?@?([^/\?]*)|", $twitterUrl, $matches);
Adding \?
to the excluded character class after the username ensures the query string is excluded.
Upvotes: 3
Reputation: 12235
There are a couple more test cases to make a universal regexp.
https
URLs are also validtwitter.com/@username
also go to username's profileThis should do the trick in PHP
preg_match("|https?://(www\.)?twitter\.com/(#!/)?@?([^/]*)|", $twitterUrl, $matches);
If preg_match
returns 1 (a match) then the result is on $matches[3]
Upvotes: 22
Reputation: 174289
Try this:
^https?://(www\.)?twitter\.com/(#!/)?(?<name>[^/]+)(/\w+)*$
The sub group "name" will contain the twitter username.
This regex assumes that each URL is on its own line.
To use it in JS, use this:
^https?://(www\.)?twitter\.com/(#!/)?([^/]+)(/\w+)*$
The result is in the sub group $3.
Upvotes: 17
Reputation: 1816
This regex matches all four given URLs. The user name is present in $1
m[twitter\.com/+(?:#!/+)?(\w+)]
Use this to check
perl -le '$_="<url>"; m[twitter\.com/+(?:#!/+)?(\w+)]; print $1'
Upvotes: 1