import re
regex = re.compile(r"m=audio.*\r?\n(?!(?:^a=.*\r?\n)*^a=rtpmap:0 pcmu/8000)(?:^a=.*\r?\n)*^a=rtpmap:18 g729/8000\r?\n(?:^a=.*\r?\n)*", flags=re.MULTILINE)
test_str = ("m=audio 16468 RTP/AVP 0 8 9 18 120 102 104 103 101\n"
"a=rtpmap:8 pcma/8000\n"
"a=rtpmap:9 g722/8000\n"
"a=rtpmap:18 g729/8000\n"
"a=fmtp:18 annexb=yes\n"
"a=rtpmap:120 opus/48000/2\n"
"a=rtpmap:102 iLBC/8000\n"
"a=rtpmap:104 iSAC/32000\n"
"a=rtpmap:103 iSAC/16000\n"
"a=rtpmap:101 telephone-event/8000\n"
"a=fmtp:101 0-15\n"
"m=audio 16468 RTP/AVP 0 8 9 18 120 102 104 103 101\n"
"a=rtpmap:0 pcmu/8000\n"
"a=rtpmap:8 pcma/8000\n"
"a=rtpmap:9 g722/8000\n"
"a=rtpmap:18 g729/8000\n"
"a=fmtp:18 annexb=yes\n"
"a=rtpmap:120 opus/48000/2\n"
"a=rtpmap:102 iLBC/8000\n"
"a=rtpmap:104 iSAC/32000\n"
"a=rtpmap:103 iSAC/16000\n"
"a=rtpmap:101 telephone-event/8000\n"
"a=fmtp:101 0-15\n"
"m=audio 16468 RTP/AVP 0 8 9 18 120 102 104 103 101\n"
"a=rtpmap:8 pcma/8000\n"
"a=rtpmap:9 g722/8000\n"
"a=rtpmap:18 g729/8000\n"
"a=fmtp:18 annexb=yes\n"
"a=rtpmap:120 opus/48000/2\n"
"a=rtpmap:102 iLBC/8000\n"
"a=rtpmap:104 iSAC/32000\n"
"a=rtpmap:103 iSAC/16000\n"
"a=rtpmap:101 telephone-event/8000\n"
"a=fmtp:101 0-15\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