import re
regex = re.compile(r"((https?:\/\/)?((www|\w\w)\.)?linkedin\.com\/)((([\w]{2,3})?)|([^\/]+\/(([\w|\d-&#?=])+\/?){1,}))$", flags=re.MULTILINE)
test_str = ("in.linkedin.com/pub/first-second/05/25/86\n"
"in.linkedin.com/pub/first-second/1/638/7a1\n"
"http://uk.linkedin.com/pub/first-second/1/1b3/b45/\n"
"http://in.linkedin.com/pub/first-second/hfk/uty\n"
"http://www.linkedin.com/in/aname/\n"
"http://linkedin.com/in/name\n"
"http://in.linkedin.com/in/name\n"
"http://in.linkedin.com/in/name/\n"
"in.linkedin.com/in/name/\n"
"https://www.linkedin.com/profile/view?id=AAIAABgM9d4ymiX9Dy4yXkQq9i1W01JsbMXp8lM&trk=nav_responsive_tab_profile\n"
"www.myweb.com?id=12&value=val\n")
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