import re
regex = re.compile((r"([\w ]+) (is friends with) ([\w ]+)\n"), flags=re.MULTILINE)
test_str = ("Jesus is friends with Chuck Norris\n"
"Cindy Crawford is friends with Nicole Kidman\n"
"V is friends with Barack Obama\n"
"Chuck Norris is friends with Barack Obama\n"
"V is friends with François Hollande\n"
"Penelope Cruiz is friends with Tom Cruise\n"
"Nicole Kidman is friends with Tom Cruise\n"
"Katie Holmes is friends with Tom Cruise\n"
"Sim is friends with Lara Croft\n"
"Sim is friends with Chuck Norris\n"
"Lara Croft is friends with V\n"
"Yvette Horner is friends with Sim\n"
"François Hollande is friends with Barack Obama\n"
"Sim is friends with Jesus\n"
"Tom Cruise is friends with Barack Obama\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