n00b
n00b

Reputation: 16536

Regex - Extract TwitterUsername from URL

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

http://www.twitter.com/#!/KourtneyKardash/following

http://twitter.com/#!/jasonterry31/lists/memberships

Upvotes: 15

Views: 11786

Answers (8)

Nikola
Nikola

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

JediTricks007
JediTricks007

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

Hugo H
Hugo H

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

Louis Simoneau
Louis Simoneau

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

Lombo
Lombo

Reputation: 12235

There are a couple more test cases to make a universal regexp.

  • https URLs are also valid
  • URLs like twitter.com/@username also go to username's profile

This 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

Daniel Hilgarth
Daniel Hilgarth

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

kshenoy
kshenoy

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

Rudie
Rudie

Reputation: 53781

This one works for me (in PHP): /twitter\.com(?:\/\#!)?\/(\w+)/i

Upvotes: 0

Related Questions