import re
regex = re.compile(r"<TextView[^f>]*(?:f(?!ontFamily)[^f>]*)*>", flags=re.MULTILINE)
test_str = ("<TextView\n"
" android:id=\"@+id/txt_refer_invite\"\n"
" android:layout_width=\"match_parent\"\n"
" android:layout_height=\"wrap_content\"\n"
" android:layout_below=\"@id/txt_refer_earn\"\n"
" android:layout_marginTop=\"8dp\"\n"
" android:fontFamily=\"@font/ms500\"\n"
" android:gravity=\"center\"\n"
" android:lineSpacingExtra=\"10sp\"\n"
" android:paddingLeft=\"16.7dp\"\n"
" android:paddingRight=\"16.7dp\"\n"
" android:text=\"@{data.description}\"\n"
" android:textColor=\"@color/color_858585\"\n"
" android:textSize=\"13.3sp\"/>\n"
"<TextView\n"
" android:id=\"@+id/txt_refer_code\"\n"
" android:layout_width=\"200dp\"\n"
" android:layout_height=\"48dp\"\n"
" android:layout_below=\"@id/txt_refer_invite\"\n"
" android:layout_centerHorizontal=\"true\"\n"
" android:layout_marginTop=\"22dp\"\n"
" android:background=\"@drawable/bg_refer_code\"\n"
" android:gravity=\"center\"\n"
" android:text=\"@{data==null?``:`Invite code : `+data.referralCode}\"\n"
" android:textColor=\"#9c9c9c\"\n"
" android:textSize=\"14.3sp\"/>")
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