import re
regex = re.compile(r"http.+(?:(?:v=)|(?:\/a\/u\/1\/)|(?:(?:v|embed|youtu\.be|shorts)\/))(?<videoId>.{11})[^<]*", flags=re.MULTILINE)
test_str = ("<p>http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index</p><p>Para</p>\n"
"<p>http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o</p><p>Para</p>\n"
"<p>http://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0</p><p>Para</p>\n"
"<p>http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s</p><p>Para</p>\n"
"<p>http://www.youtube.com/embed/0zM3nApSvMg?rel=0</p><p>Para</p>\n"
"<p>http://www.youtube.com/watch?v=0zM3nApSvMg</p><p>Para</p>\n"
"<p>http://youtu.be/0zM3nApSvMg</p><p>Para</p>\n"
"<p>https://youtube.com/shorts/0dPkkQeRwTI?feature=share</p><p>Para</p>\n"
"<p>https://youtube.com/shorts/0dPkkQeRwTI</p><p>Para</p>\n"
"<p>https://www.youtube.com/watch?v=0dPkkQeRwTI&ab_channel=RelaxedChan</p><p>Para</p>")
subst = "<iframe width=\"100%\" src=\"https://www.youtube.com/embed/$videoId\" frameborder=\"0\" allowfullscreen></iframe>"
result = regex.sub(subst, test_str)
if result:
print(result)
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