I want to print out the first html tags thats has attributes
test
test2
This seems pretty complicated, you can try with this expression, but it would fail in some cases. It would first collect the undesired instances, then at the end there is a capturing group for those desired.
Maybe, it wouldn't be the best idea to use regular expressions here.
import re
regex = r"^\s*<\S+>\s*$|^\s*<\S+\s.*test.*?>.*?<\/\S+>$|^\s*(<.*>)\s*$"
test_str = """
test
test2
test3
test3
"""
print(re.findall(regex, test_str, re.M))
['', '', '', '', '', '', '']
The expression is explained on the top right panel of regex101.com, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs, if you like.