# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"\b(?:silence|black)_(?:start|end|duration):\s*\d+(?:\.\d+)?\b"
test_str = ("ffmpeg version 4.2.2 Copyright (c) 2000-2019 the FFmpeg developers\n"
" built with Apple clang version 11.0.0 (clang-1100.0.33.17)\n"
" configuration: --prefix=/usr/local/Cellar/ffmpeg/4.2.2_3 --enable-shared --enable-pthreads --...\n"
"[silencedetect @ 0x7fdd82d011c0] silence_start: 0\n"
"frame= 112 fps=0.0 q=-0.0 size=N/A time=00:00:05.00 bitrate=N/A speed=9.96x \n"
"[blackdetect @ 0x7fdd82e06580] black_start:0 black_end:5 black_duration:5\n"
"[silencedetect @ 0x7fdd82d011c0] silence_end: 5.06285 | silence_duration: 5.06285\n"
"frame= 211 fps=210 q=-0.0 size=N/A time=00:00:09.00 bitrate=N/A speed=8.97x \n"
"frame= 319 fps=212 q=-0.0 size=N/A time=00:00:13.00 bitrate=N/A speed=8.63x \n"
"frame= 427 fps=213 q=-0.0 size=N/A time=00:00:17.08 bitrate=N/A speed=8.51x \n"
"frame= 537 fps=214 q=-0.0 size=N/A time=00:00:22.00 bitrate=N/A speed=8.77x \n"
"frame= 650 fps=216 q=-0.0 size=N/A time=00:00:26.00 bitrate=N/A speed=8.63x \n"
"frame= 761 fps=217 q=-0.0 size=N/A time=00:00:31.00 bitrate=N/A speed=8.82x \n"
"frame= 874 fps=218 q=-0.0 size=N/A time=00:00:35.00 bitrate=N/A speed=8.71x \n"
"frame= 980 fps=217 q=-0.0 size=N/A time=00:00:39.20 bitrate=N/A speed=8.67x \n"
"... \n"
"frame= 5680 fps=213 q=-0.0 size=N/A time=00:03:47.20 bitrate=N/A speed=8.53x \n"
"[silencedetect @ 0x7fdd82d011c0] silence_start: 227.733\n"
"[silencedetect @ 0x7fdd82d011c0] silence_end: 229.051 | silence_duration: 1.3184\n"
"[silencedetect @ 0x7fdd82d011c0] silence_start: 229.051\n"
"[blackdetect @ 0x7fdd82e06580] black_start:229.28 black_end:230.24 black_duration:0.96\n"
"frame= 5757 fps=214 q=-0.0 Lsize=N/A time=00:03:50.28 bitrate=N/A speed=8.54x \n"
"video:3013kB audio:43178kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown\n"
"[silencedetect @ 0x7fdd82d011c0] silence_end: 230.28 | silence_duration: 1.22856")
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