import re
regex = re.compile(r"(\d+/\d+/\d+, \d+:\d+\d+) - (.+?): (.*)")
test_str = ("20/01/18, 08:11 - Peter: Good morning \n\n"
"How are you?\n\n"
"Peter\n"
"20/01/18, 09:00 - Caroline: I am fine thanks.\n"
"You?\n\n"
"20/01/18, 09:01 - Peter: Good\n\n"
"I had some problems few days ago.\n\n"
"Now I am happy\n\n"
"Are you working?\n\n"
"20/01/18, 09:02 - Caroline: No I have to go to the supermarket to buy vegetables\n\n"
"20/01/18, 09:12 - Peter: Nice!\n\n"
"Where are you now?")
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