Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

REGEX Help for Twitter

Posted on 2014-03-18
6
Medium Priority
?
527 Views
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!

http://twitter.com/username/ 
http://twitter.com/username 

https://twitter.com/username/ 
https://twitter.com/username 

http://twitter.com/#!/username/
https://twitter.com/#!/username/

http://twitter.com/intent/user?screen_name=username
https://twitter.com/intent/user?screen_name=username
0
Comment
Question by:EffinGood
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39938616
.*\b(\w+)
0
 
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.
0
 

Author Comment

by:EffinGood
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 35

Accepted Solution

by:
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:
<?php

$urls = array(
"http://twitter.com/username/",
"http://twitter.com/username",
"https://twitter.com/username/",
"https://twitter.com/username",
"http://twitter.com/#!/username/",
"https://twitter.com/#!/username/",
"http://twitter.com/intent/user?screen_name=username",
"https://twitter.com/intent/user?screen_name=username");

foreach($urls as $url)
{
  echo "URL: {$url}\n";
  preg_match("@https?://twitter.com/(?:#!/|intent/user\?screen_name=)?(\w+)@",$url,$matches);
  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

0
 

Author Closing Comment

by:EffinGood
ID: 39938669
Nice! Thank you! I remember that nifty piece of software now - it's been eons! Thank you very much Gonzo!
0
 
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);
print_r($matches[1]);

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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

604 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