Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


REGEX Help for Twitter

Posted on 2014-03-18
Medium Priority
Last Modified: 2014-03-18
Hello guys,

Can you help me with the REGEX for the following?

I want to extract the "username" out of any of the following strings. The main differences are http[s], trailing slash, the inclusion of #!, and a variant that includes some other stuff in the url.

Thank you!




Question by:EffinGood
LVL 85

Expert Comment

ID: 39938616
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 39938646
If you are using php, there is an easier way (assuming that you are showing all possible formats).

Just explode() the string on "/", take the 4th part.  It will be either the username or "intent" or "#!".  In the second case, explode again on "=" and the last part will be the username.  In the third case, take the original 5th part.

The php explode() function is awesome.

Author Comment

ID: 39938648
Well, the problem is that the strings are located in a big file full of other chars, so I need to examine the entire file to pull these out.

There will be other urls in there - I'm just looking for those with the 'twitter' part.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 36

Accepted Solution

gr8gonzo earned 2000 total points
ID: 39938657
I highly recommend installing the free tool "The Regex Coach" - I've been using it for years.

That said, try this on:

$urls = array(

foreach($urls as $url)
  echo "URL: {$url}\n";
  echo "Username: " . $matches[1] . "\n";

Open in new window

You should get this result:
URL: http://twitter.com/username/
Username: username
URL: http://twitter.com/username
Username: username
URL: https://twitter.com/username/
Username: username
URL: https://twitter.com/username
Username: username
URL: http://twitter.com/#!/username/
Username: username
URL: https://twitter.com/#!/username/
Username: username
URL: http://twitter.com/intent/user?screen_name=username
Username: username
URL: https://twitter.com/intent/user?screen_name=username
Username: username

Open in new window


Author Closing Comment

ID: 39938669
Nice! Thank you! I remember that nifty piece of software now - it's been eons! Thank you very much Gonzo!
LVL 35

Expert Comment

by:Terry Woods
ID: 39938670
Very similar to @gr8gonzo's, though I wrote it independently:
preg_match_all("@https?://twitter.com/(?:#!/)?(?:.*\?screen_name=)?((?!intent)\w+)@i", $data, $matches);

Open in new window


Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question