# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^(\d{2}:\d{2}:\d{2}[.,]\d{3})\s-->\s(\d{2}:\d{2}:\d{2}[.,]\d{3})\n(.*(?:\r?\n(?!\r?\n).*)*)"
test_str = ("WEBVTT\n\n"
"1\n"
"00:00:00.310 --> 00:00:02.540\n"
"Dans cette séquence, nous\n"
"allons voir pourquoi nos\n\n"
"2\n"
"00:00:02.550 --> 00:00:05.210\n"
"assistants personnels,\n"
"smartphones et tablettes, sont\n\n"
"3\n"
"00:00:05.210 --> 00:00:07.420\n"
"devenus tout\n"
"naturellement, en une dizaine d'années\n\n"
"4\n"
"00:00:07.420 --> 00:00:10.100\n"
"seulement, un point de collecte\n"
"majeur de données personnelles.\n\n"
"5\n"
"00:00:10.520 --> 00:00:13.140\n"
"Pourquoi le smartphone\n"
"intéresse-t-il tant de monde ?\n\n"
"6\n"
"00:00:15.690 --> 00:00:17.550\n"
"Tout d'abord, il faut\n"
"être conscient que dans un\n\n"
"7\n"
"00:00:17.550 --> 00:00:20.110\n"
"smartphone, 2 niveaux\n"
"existent. Il y a la partie visible\n\n"
"8\n"
"00:00:20.150 --> 00:00:23.440\n"
"avec son processeur dont on\n"
"va vanter les performances,\n\n"
"9\n"
"00:00:23.440 --> 00:00:26.260\n"
"le système d'exploitation\n"
"- dans ce module, nous nous")
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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