import re
regex = re.compile(r"(\$INGGA.*\n\$INHDT.*\n)")
test_str = ("$INGGA,091124.00,5249.8336,N,00120.9619,W,1,20,0.6,95.0,M,49.4,M,,*50\n"
"$INHDT,31.9,T*1E $INZDA,091124.0055,06,05,2016,,*7F\n"
"$INVTG,22.0,T,,M,4.4,N,8.1,K,A*24 \n"
"$PRDID,2.13,-0.06,34.09*6C\n"
"$INGGA,091124.20,5249.8338,N,00120.9618,W,1,20,0.6,95.0,M,49.4,M,,*5D\n"
"$INHDT,34.1,T*13 $INZDA,091124.2055,06,05,2016,,*7D\n"
"$INVTG,24.9,T,,M,4.4,N,8.1,K,A*2B \n"
"$PRDID,2.16,-0.03,36.24*61\n"
"$INGGA,091124.40,5249.8340,N,00120.9616,W,1,20,0.6,95.0,M,49.4,M,,*5A\n"
"$INHDT,36.3,T*13 $INZDA,091124.4055,06,05,2016,,*7B\n"
"$INVTG,27.3,T,,M,4.4,N,8.1,K,A*22 \n"
"$PRDID,2.11,-0.05,38.33*68\n"
"$INGGA,091124.60,5249.8343,N,00120.9614,W,1,20,0.6,95.1,M,49.4,M,,*58\n"
"$INHDT,38.4,T*1A $INZDA,091124.6055,06,05,2016,,*79\n"
"$INVTG,29.5,T,,M,4.4,N,8.1,K,A*2A \n"
"$PRDID,2.09,-0.02,40.37*6D\n"
"$INGGA,091124.80,5249.8345,N,00120.9612,W,1,20,0.6,95.1,M,49.4,M,,*56\n"
"$INHDT,40.4,T*15 $INZDA,091124.8055,06,05,2016,,*77\n"
"$INVTG,31.7,T,,M,4.4,N,8.1,K,A*21 \n"
"$PRDID,2.09,0.02,42.42*40\n"
"$INGGA,091125.00,5249.8347,N,00120.9610,W,1,20,0.6,95.1,M,49.4,M,,*5F\n"
"$INHDT,42.4,T*17\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