$re = '/(?:(?:http|https):\/\/)?(?:www.|m.)?facebook.com\/(?!home.php)(?:(?:\w)*#!\/)?(?:pages\/)?(?:[?\w\-]*\/)?(?:profile.php\?id=(?=\d.*))?([\w\.-]+)/m';
$str = 'http://www.facebook.com/jquery2dotnet
http://www.facebook.com/some.user
https://www.facebook.com/#!/my_page_id
http://www.facebook.com/pages/test
http://www.facebook.com/pages/test/123
https://www.facebook.com/#!/page_with_1_number
http://www.facebook.com/bounce_page#!/pages/test-Url/12345
https://www.facebook.com/bounce_page#!/my_page_id?v=app_1123325
https://www.facebook.com/notes/hyperarts-web-design
http://www.facebook.com/test.username
https://www.fb.me/jquery2dotnet
http://www.facebook.com/
http://m.facebook.com/home.php
http://m.facebook.com/test.username
https://mbasic.facebook.com/home.php?_rdr';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php