import re
regex = re.compile(r" \K\S+=[\"']?[^>\"']+[\"']?>??", flags=re.DOTALL)
test_str = ("<anytag aa=\"att1\">DESC 1</anytag>\n"
"<item aa=\"att2\">DESC 2</item>\n"
"<anytag bb=\"att3\">DESC 3</anytag>\n"
"<anytag alt=att4>DESC 4</anytag>\n"
"<anytag src=\"att5\">DESC 5</anytag>\n"
"<anytag src=\"att6\">DESC 6</anytag>\n"
"<anytag src='att7'>DESC 7</anytag>\n"
"<anytag src='att8'>DESC 8</anytag>\n"
"<anytag href=\"att9\" title=\"title1\">DESC 9</anytag>\n"
"<anytag blabla=\"att10\">DESC 10</anytag>")
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