import re
regex = re.compile(r"(?s)(\#\d{1,})(.*?)(\#\d{1,})", flags=re.MULTILINE)
test_str = ("#0\n"
"$dumpvars\n"
"0!\n"
"0\"\n"
"0#\n"
"bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 7\n"
"bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 6\n"
"bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 5\n"
"b0000000000000000 $\n"
"bxxxxxxxxxxxxxxxx /\n"
"bxxxxxxxxxxxxxxxx .\n"
"bxxxxxxxxxxxxxxxx )\n"
"b0111111111111111 %\n"
"bxxxxxxxxxxxxxxxx 1\n"
"bxxxxxxxxxxxxxxxx 0\n"
"bxxxxxxxxxxxxxxxx *\n"
"b10101010101010101010101010101010 &\n"
"bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +\n"
"bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ,\n"
"bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 2\n"
"bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx -\n"
"bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 3\n"
"bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 4\n"
"bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (\n"
"bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx '\n"
"$end\n"
"#600\n"
"1!\n"
"b0000000000000000 )\n"
"b0111111111111111 *\n"
"b10101010101010101010101010101010 +\n"
"b0000000000000000 /\n"
"b0111111111111111 1\n"
"b00000000000000000000000000000000 5\n"
"b10101010101010101010101010101010 4\n"
"b00000000000000000000000000000000 2\n"
"b00000000000000000000000000000000 3\n"
"b010101010101010101010101010101010 7\n"
"#1200\n"
"b0000010001010111 $\n"
"b0111101110101000 %\n"
"b10101100101001110100001001010001 &\n"
"0!\n"
"b10101010101010101010101010101010 ,\n"
"b00000000000000000000000000000000 -\n"
"#1800\n"
"1!\n"
"b0000010001010111 )\n"
"b0111101110101000 *\n"
"b10101100101001110100001001010001 +\n"
"b010101010101010101010101010101010 (\n"
"b010101010101010101010101010101010 '\n"
"b0000010001010111 /\n"
"b00000010001010110111101110101001 5\n"
"b0111101110101000 1\n"
"b00000010000110001010011000011000 5\n"
"b10101100101001110100001001010001 4\n"
"b010101100101001110100001001010001 7\n"
"b00000010000110001010011000011000 2\n"
"b00000010000110001010011000011000 3\n"
"b010101110101111111110100001101001 7\n"
"#2400\n"
"b0000100010101110 $\n"
"b0111011101010001 %\n"
"b10101110101000111101100111111000 &\n"
"0!\n"
"b10101100101001110100001001010001 ,\n"
"b00000010000110001010011000011000 -\n"
"#3000\n"
"1!\n"
"b0000100010101110 )\n"
"b0111011101010001 *\n"
"b10101110101000111101100111111000 +\n"
"b010101110101111111110100001101001 (\n"
"b010101110101111111110100001101001 '\n"
"b0000100010101110 /\n"
"b00000100001100010100110000110000 5\n"
"b0111011101010001 1\n"
"b00000100000010111010000100001110 5\n"
"b10101110101000111101100111111000 4\n"
"b010110000101111001000000000010000 7\n"
"b00000100000010111010000100001110 2\n"
"b00000100000010111010000100001110 3\n"
"b010110010101011110111101100000110 7\n"
"#3600\n"
"b0000110100000101 $\n"
"b0111001011111010 %\n"
"b10110000101000000111000110011111 &\n"
"0!\n"
"b10101110101000111101100111111000 ,\n"
"b00000100000010111010000100001110 -\n"
"#4200")
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