import re
regex = re.compile(r"""
<link(?=[^<>]*?opensearch)(?:[^h]+|h(?!ref))*+href=["']?\K([^<>'"\s]+(?:\/(?!>)|\b))(?=[^<>]*>)
""", flags=re.IGNORECASE | re.MULTILINE | re.VERBOSE)
test_str = ("This should succeed:\n"
"<link rel=\"search\" type=\"application/opensearchdescription+xml\" href=\"http://example.com/tsr-opensearch/\" title=\"\" />\n"
"<link rel=\"search\" type=\"application/opensearchdescription+xml\" href=\"http://www.example.com/search.xml\" title=\"\" />\n"
"<link rel=\"search\" type=\"application/opensearchdescription+xml\" href=\"/search.xml\" title=\"\" />\n"
"<link rel=\"search\" type=\"application/opensearchdescription+xml\" href=\"search.xml\" title=\"\" />\n"
"<link rel=\"search\" type=\"application/opensearchdescription+xml\" href=\".search.xml\" title=\"\" />\n"
"<link rel=\"search\" type=\"application/opensearchdescription+xml\" href=\"./search.xml\" title=\"\" />\n"
"<link rel=\"search\" type=\"application/opensearchdescription+xml\" href=\".search.xml\" title=\"\" />\n"
"<link rel=\"search\" type=\"application/opensearchdescription+xml\" href='http://www.example.com/search.xml' title=\"\" />\n"
"<link rel=\"search\" type=\"application/opensearchdescription+xml\" href=http://www.example.com/search.xml title=\"\" />\n"
"<link rel=\"search\" type=\"application/opensearchdescription+xml\" href=http://www.example.com/search.xml>\n"
"<link rel=\"search\" type=\"application/opensearchdescription+xml\" href=http://www.example.com/search.xml />\n"
"<link rel=\"search\" type=\"application/opensearchdescription+xml\" href=http://www.example.com/search.xml />\n"
"<link rel=\"search\" type=\"application/opensearchdescription+xml\" href=http://www.example.com/search.xml/>\n"
"<link rel=\"search\" type=\"application/opensearchdescription+xml\" href=http://www.example.com/dsfdsf/search.xml />\n"
"<link rel=\"search\" type=\"application/opensearchdescription+xml\" href=http://www.example.com/search.xml/ />\n"
"<link rel=\"search\" type=\"application/opensearchdescription+xml\" href=http://www.example.com/search.xml/ />\n"
"<link rel=\"search\" type=\"application/opensearchdescription+xml\" href=http://href.example.com/search.xml/ />\n"
"<link rel=\"search\" type=\"application/opensearchdescription+xml\" hre href=http://href.example.com/search.xml/ />\n"
"<link rel=\"search\" type=\"application/opensearchdescription+xml\" ref=\"adfsf\" href=http://href.example.com/search.xml/ />\n\n"
"Should fail:\n"
"<link rel=\"stylesheet\" type=\"text/css\" href=\"theme.css\">\n"
"<link rel=\"search\" type=\"application/opensearchdescription+xml\" href= http://www.example.com/search.xml />\n"
"<link rel=\"search\" type=\"application/opensearchdescription+xml\" href= http://www.example.com/search.xml>\n"
"link rel=\"search\" type=\"application/opensearchdescription+xml\" href=http://www.example.com/search.xml>\n"
"link rel=\"search\" type=\"application/opensearchdescription+xml\" href=http://www.example.com/search.xml />\n"
"<link rel=\"search\" type=\"application/opensearchdescription+xml\" href=http://www.example.com/search.xml")
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