import re
regex = re.compile(r"^(([A-Za-z\d]+[\-_])*[A-Za-z\d]{2,}\.)*(?:testnet|near)$", flags=re.MULTILINE)
test_str = ("// VALID\n"
"example.near\n"
"example.testnet\n"
"sub.xtrtarstsrt3322.testnet\n"
"1sub.sub.near\n"
"12.near\n"
"aa.testnet\n"
"1a.a3a.near\n"
"deltron-me.testnet\n"
"deltron_yes.near\n"
"// INVALID because of the capital letter but we can lowercase it on db\n"
"Fmish.testnet\n\n"
"// INVALID\n"
"aa.setetstestnet\n\n"
"_sentaent.testnet\n"
"-howabout.this.near\n"
"space should fail.testnet\n"
"space should fail.near\n"
"!testnet.testnet\n"
"2a!.near\n"
"n.testnet\n"
"shouldnotendwithperiod.testnet.\n"
"aa.t.testnet\n"
"1a..near\n"
"touchingDotsShouldfail..testnet\n"
".aa.testnet")
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