import re
regex = re.compile(r"(?:(?:http|https):\/\/)?(?:www.|m.)?facebook.com\/(?!home.php)(?:(?:\w)*#!\/)?(?:pages\/)?(?:[?\w\-]*\/)?(?:profile.php\?id=(?=\d.*))?([\w\.-]+)", flags=re.MULTILINE)
test_str = ("http://www.facebook.com/jquery2dotnet\n"
"http://www.facebook.com/some.user\n"
"https://www.facebook.com/#!/my_page_id\n"
"http://www.facebook.com/pages/test\n"
"http://www.facebook.com/pages/test/123\n"
"https://www.facebook.com/#!/page_with_1_number\n"
"http://www.facebook.com/bounce_page#!/pages/test-Url/12345\n"
"https://www.facebook.com/bounce_page#!/my_page_id?v=app_1123325\n"
"https://www.facebook.com/notes/hyperarts-web-design\n"
"http://www.facebook.com/test.username\n"
"https://www.fb.me/jquery2dotnet\n"
"http://www.facebook.com/\n"
"http://m.facebook.com/home.php\n"
"http://m.facebook.com/test.username\n"
"https://mbasic.facebook.com/home.php?_rdr")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
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 Python, please visit: https://docs.python.org/3/library/re.html