import re
regex = re.compile(r"<a href=\"javascript:void\(0\)\"[^>]*>(.*?)id=\"sw-slots-grid-line-break\"", flags=re.IGNORECASE | re.DOTALL)
test_str = ("<div id=\"tmm-grid-swatch-HARDCOVER\" class=\"a-column a-span6 a-text-left swatchElement selected celwidget\" role=\"listitem\" data-csa-c-id=\"rqzqyx-arj8qg-jifnrc-pelgtz\" data-cel-widget=\"tmm-grid-swatch-HARDCOVER\"> <span class=\"a-button a-button-selected a-spacing-none a-button-toggle format\" id=\"a-autoid-2\"><span class=\"a-button-inner\"><a href=\"javascript:void(0)\" role=\"radio\" aria-checked=\"true\" aria-current=\"page\" class=\"a-button-text a-text-left\" id=\"a-autoid-2-announce\"> <span class=\"slot-title\">\n"
" <span aria-label=\"Hardcover Format:\">Hardcover</span> <br> </span>\n"
" <span class=\"slot-price\">\n"
" <span aria-label=\"$16.94\" class=\"a-size-base a-color-price a-color-price\"> $16.94 </span> </span>\n"
" <br id=\"sw-slots-grid-line-break\">\n\n"
" </a></span></span> </div>")
match = regex.search(test_str)
if match:
print(f"Match 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