import re
regex = re.compile(r"\/(\w+)Box\ \[\ ?([-]?\d+|[-]?\d+\.\d+) +([-]?\d+|[-]?\d+\.\d+) +([-]?\d+|[-]?\d+\.\d+)\ +([-]?\d+|[-]?\d+\.\d+)\ *?\]", flags=re.MULTILINE)
test_str = ("8 0 obj\n"
"<< \n"
"/Type /Page \n"
"/Parent 3 0 R \n"
"/Resources 9 0 R \n"
"/Contents [ 40 0 R 42 0 R 44 0 R 51 0 R 53 0 R 55 0 R 57 0 R 59 0 R ] \n"
"/TrimBox [ 42 42 2151 1573 ] \n"
"/BleedBox [ 36 36 2157 1579 ] \n"
"/MediaBox [ 0 0 2194 1615 ] \n"
"/CropBox [ 0 0 2194 1615 ] \n"
"/Rotate 0 \n"
">> \n"
"endobj")
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