import re
regex = re.compile(r"(?<=\w{7}\s\[)[^\]]+", flags=re.MULTILINE)
test_str = (" fifthrelease 566091c changes to labranges] (3rd revision) loaded\n"
" firstrelease 4a56940 [origin/firstrelease] change priority to 1100 for all OpenQuery DVPs\n"
" fourthrelease 2875c7b [origin/fourthrelease] all DVPs loaded (including new formulas); rev 21 CRF\n"
" main 566091c [origin/main] changes to labranges (3rd revision) loaded\n"
" secondrelease ff35618 [origin/secondrelease] fixed double SAE mail generated by AE_09\n"
"* sixthrelease 440478f [origin/sixthrelease] 4th lab revision processed and loaded\n"
" thirdrelease e3d7038 [origin/thirdrelease] LabRanges loaded")
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