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:
Result 2:
Result 3:
The ideal outcome would look like:
Result 1:
Result 2:
Result 3:
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.
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.
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.
For example, a status object and its comments can be reached via a URL of the form: http://www.facebook.com/facebookAccountID/posts/facebookStatusID.
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\-]*)?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With