26

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.

5
  • This smells like a facebook scraping script. Mar 5, 2011 at 23:55
  • @Brad does everything with the word facebook in it smeel like that to you ? i think not enough info to determine what the script using this would be doing
    – n00b
    Mar 6, 2011 at 0:11
  • 4
    @Brad It's not, but why should you care if it is or if it isn't? Mar 6, 2011 at 0:49
  • @noob32: Nope, just typically url parsing with a full API avail. (reminds me of the mass "Add as a friend" scripts that were on myspace, TBH) -- @JoshDeeden: Why should I care? Given the 6 degree rule, you could grab a lot of names off facebook just going through friend lists. Not that brute forcing the 6 billion numeric id's wouldn't get you somewhere as well, but I just don't see a reason you'd need to parse a url (not that there isn't a fully-legitimate use for it). Almost everything you'd need is in the API. Mar 6, 2011 at 2:31
  • 1
    parsing the url is much simpler than using the API...
    – n00b
    Mar 6, 2011 at 2:48

8 Answers 8

24

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\-]*)?
7
  • 1
    Awesome. Thanks a ton. That almost works. The matches are retured in the right position, but facebook.com/ still matches. That's my fault. In my original post, I stated that facebook.com shouldn't match (and it doesn't), but failed to mention that facebook.com/ shouldn't match either. Ideally matches would only be returned when a complete profile URL is given... Mar 6, 2011 at 1:21
  • 7
    Seems the regex does not recognize urls like facebook.com/test.username . Any ideas?
    – SteMa
    Dec 2, 2011 at 18:16
  • 1
    It seems to work quite well for me, apart from the fact that it doesn't take into account https url, which are now the norm.
    – damusnet
    Aug 10, 2012 at 15:59
  • 3
    To match facebook.com/test.username I guess it needs to parse . chars as well with - for example (?:(?:http|https):\/\/)?(?:www.)?facebook.com\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[?\w\-]*\/)?(?:profile.php\?id=(?=\d.*))?([\w\.-]*)?
    – ralixyle
    Dec 21, 2017 at 9:48
  • No need to check http in url because facebook has https so check https only in (?:(?:http|https):....
    – w.Daya
    Oct 22, 2021 at 5:56
11

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

1
  • This one is not matching profiles with a . in the name like www.facebook.com/my.name and it includes www.facebook.com/ which is just FB and not a profile page.
    – spa900
    Jul 3, 2019 at 2:25
8

Only this regular expression works correctly for all FB URLs:

/(?:https?:\/\/)?(?:www\.)?(?:facebook|fb|m\.facebook)\.(?:com|me)\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[\w\-]*\/)*([\w\-\.]+)(?:\/)?/i
6

I've tried every single answer above and each one doesn't work for at least one reason. This most likely won't be helpful to OP, but if anybody like me finds this in a web search, I believe this is the correct answer:

^(?:.*)\/(?:pages\/[A-Za-z0-9-]+\/)?(?:profile\.php\?id=)?([A-Za-z0-9.]+)

Supports basically everything I can think of, except verifying that the domain contains facebook.com. If you need to check if the URL is valid, this should be done outside of a regular expression to make sure the page or profile actually exists. Why check it twice, especially when one of the checks is incomplete?

  • Doesn't cut off the first character
  • Grabs URLs with periods
  • Ignores superfluous GET parameters
  • Supports /usernames as provided by the Facebook app
  • Supports both profile URL structures
  • Doesn't match facebook.com/ or facebook.com (by ignoring them)
  • Works with and without www. (by ignoring it)
  • Supports both http and https (by ignoring them)
  • Supports both facebook.com and fb.com (by ignoring them)
  • Supports pages with special characters in the name (by ignoring them)
  • Supports #! (by ignoring it)
  • Supports bounce_page#! (by ignoring it)
2
  • Should that say album instead of alnum?
    – Taraz
    Jun 1, 2020 at 22:09
  • 1
    [:alnum:] is a POSIX-ism that's equivalent to [A-Za-z0-9]. When I wrote this answer 4 years ago I wrote a lot of PHP and thus was used to having PCRE character classes, which you probably won't have access to if you're not working in Bash or PHP. So I'll swap it out, thanks for the bump. Jun 2, 2020 at 2:11
5

The most completed pattern for Facebook profile url:

/(?:https?:\/\/)?(?:www\.)?facebook\.com\/.(?:(?:\w)*#!\/)?(?:pages\/)?(?:[\w\-]*\/)*([\w\-\.]*)/

It detects all the cases + one important difference. Other regex patterns recognize http://www.facebook.com/ as a valid Facebook Profile URL while it is not a valid Profile url. It is just the original Facebook URL and not a user or page address. But this regex can distinguish a normal url from a profile and page url and only accepts the valid one.

5

Matches facebook.com, m.facebook.com, mbasic.facebook.com and fb.me (short link)

/(?:https?:\/\/)?(?:www\.)?(mbasic.facebook|m\.facebook|facebook|fb)\.(com|me)\/(?:(?:\w\.)*#!\/)?(?:pages\/)?(?:[\w\-\.]*\/)*([\w\-\.]*)/ig

Facebook URL regex DEMO

Facebook URL regex DEMO

0

Regex that will correctly identify profile pages with a . in the name like www.facebook.com/my.name and it will also exclude www.facebook.com/ or home.php as it is not a valid facebook page.

https://regex101.com/r/koN8C2/2

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

Let me know if you found any that are not matched.

-2

This works well for me. It can detect personal profile url, and exclude all the fan pages, and groups.

.+www.facebook.com\/[^\/]+$

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.