import re
regex = re.compile(r"(<\/?img.*[^-]width\s?[:|=]\s?['|\"]?)[0-9]+px(.*)>")
test_str = ("<img >\n"
"<img />\n"
"<img >width\n"
"<img width >\n"
"<img src = \"\">\n"
"<strong><img width=\"500px\" style = 'color:5px' width=\"500px;\" /></strong>\n"
"<img style = 'height:50px;' width=500px />\n"
"<strong><img style = 'color:5px' width=\"5000px;\" /></strong>\n"
"<img style = 'color:5px;width:265px;' />\n"
"<img style = 'color:5px;width:50%;' />\n"
"<img style = 'width:50%;' />\n"
"<img style = 'height:50px;' />\n"
"<img style=\"width: 5px;\" />")
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