Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook profile URL regular expression

Tags:

Given the following Facebook profile and page URLs, my intent is to extract profile ids or usernames into the first match position.

http://www.facebook.com/profile.php?id=123456789   http://www.facebook.com/someusername   www.facebook.com/pages/Regular-Expressions/207279373093   

The regex I have so far looks like this:

(?:http:\/\/)?(?:www.)?facebook.com\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[?\w\-]*\/)?(?:profile.php\?id=(\d.*))?([\w\-]*)? 

Which produces the following results:

Result 1:

  1. 123456789
  2.  

Result 2:

  1.  
  2. someusername

Result 3:

  1.  
  2. 207279373093

The ideal outcome would look like:

Result 1:

  1. 123456789

Result 2:

  1. someusername

Result 3:

  1. 207279373093

That is to say, I'd like to have the profile identifier to always be returned in the first position.

It would also be ideal of www.facebook.com/ and facebook.com/ didn't match either.

like image 474
Josh Deeden Avatar asked Mar 05 '11 17:03

Josh Deeden


People also ask

How do I find the regex for a URL?

Match the given URL with the regular expression. In Java, this can be done by using Pattern. matcher(). Return true if the URL matches with the given regular expression, else return false.

How do I know what my Facebook URL is?

You can find a Facebook URL in the address bar at the top of the browser if you are using a computer. To find the URL for a personal page in the mobile app, tap the three-dot menu and find the address in the Profile link section.

What is the Facebook URL format?

For example, a status object and its comments can be reached via a URL of the form: http://www.facebook.com/facebookAccountID/posts/facebookStatusID.


2 Answers

I'd recommend Rad Software Regular Expression Designer.

Also this online tool is great https://regex101.com/ ( though most people prefer http://regexr.com/ )

(?:(?:http|https):\/\/)?(?:www.)?facebook.com\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[?\w\-]*\/)?(?:profile.php\?id=(?=\d.*))?([\w\-]*)? 
like image 149
n00b Avatar answered Oct 13 '22 06:10

n00b


I made a gist a while back that works fine against the given examples:

# Matches patterns such as: #    http://www.facebook.com/my_page_id => my_page_id #    http://www.facebook.com/#!/my_page_id => my_page_id #    http://www.facebook.com/pages/Paris-France/Vanity-Url/123456?v=app_555 => 45678 #    http://www.facebook.com/pages/Vanity-Url/45678 => 45678 #    http://www.facebook.com/#!/page_with_1_number => page_with_1_number #    http://www.facebook.com/bounce_page#!/pages/Vanity-Url/45678 => 45678 #    http://www.facebook.com/bounce_page#!/my_page_id?v=app_166292090072334 => my_page_id  /(?:http:\/\/)?(?:www\.)?facebook\.com\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[\w\-]*\/)*([\w\-]*)/ 

To get the latest version: https://gist.github.com/733592

like image 37
marcgg Avatar answered Oct 13 '22 06:10

marcgg