import re
regex = re.compile(r"(?:<img|(?<!^)\G)\h*([-\w]+)=\"([^\"]+)\"(?=.*?\/>)", flags=re.MULTILINE)
test_str = ("<p>List of sample images.</p>\n"
"<img src=\"https://placehold.it/250x100.jpg\" width=\"250\" height=\"100\" alt=\"Dimensions Set\" />\n"
"<img src=\"https://placehold.it/250x100/99cc00/000.jpg?text=JPG\" alt=\"JPG\" /><br>\n"
"<img src=\"https://placehold.it/250x100.gif?text=GIF\" alt=\"GIF\" /><br>\n"
"<img src=\"https://placehold.it/250x100/ff6600/000.png?text=PNG\" alt=\"PNG\" /><br>\n"
"<img class=\"no-ext\" src=\"https://placehold.it/350x150?text=No Extension\" alt=\"No Ext\" /><br>\n"
"<img src=\"https://placehold.it/250x100.png\" custom-attr=\"custom1\" another-attr=\"custom2\" /><br>\n"
"<img class=\"svg\" src=\"https://upload.wikimedia.org/wikipedia/commons/0/02/SVG_logo.svg\" alt=\"SVG\" /><br>\n"
"<img class=\"webp\" src=\"https://gstatic.com/webp/gallery/1.webp\" width=\"100\" alt=\"webP\" /><br>")
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