# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?<=(?:\[embed\])|(?:\[embed\]\s)|(?:))(https?:\/\/)(:?www\.)?(:?youtube\.com\/watch|youtu\.be\/)([\w\?=\&]+)(?=(?:\[\/embed\])|(?:\s\[\/embed\])|(?:))"
test_str = ("bla https://www.youtube.com/watch?v=Vpg9yizPP_g\n"
"http://www.youtube.com/watch?v=Vpg9yizPP_g bla\n"
"[embed]https://www.youtube.com/watch?v=Vpg9yizPP_g[/embed] bla\n"
"[embed] https://www.youtube.com/watch?v=Vpg9yizPP_g [/embed]\n"
"[embed]http://www.youtube.com/watch?v=Vpg9yizPP_g[/embed]\n"
"[embed] http://www.youtube.com/watch?v=Vpg9yizPP_g [/embed]\n"
"bla https://youtube.com/watch?v=Vpg9yizPP_g bla\n"
"http://youtube.com/watch?v=Vpg9yizPP_g\n"
"[embed]https://youtube.com/watch?v=Vpg9yizPP_g[/embed]bla\n"
"bla[embed] https://youtube.com/watch?v=Vpg9yizPP_g [/embed]\n"
"[embed]http://youtube.com/watch?v=Vpg9yizPP_g[/embed]\n"
"[embed] http://youtube.com/watch?v=Vpg9yizPP_g [/embed]\n"
"https://www.youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g\n"
"http://www.youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g\n"
"[embed]https://www.youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g[/embed]\n"
"[embed] https://www.youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g [/embed]\n"
"[embed]http://www.youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g[/embed]\n"
"bla[embed] http://www.youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g [/embed]bla\n"
"https://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g bla\n"
"bla http://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g\n"
"[embed]https://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g[/embed]\n"
"[embed] https://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g [/embed]\n"
"[embed]http://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g[/embed]\n"
"[embed] http://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g [/embed]\n"
"https://youtu.be/Vpg9yizPP_g\n"
"http://youtu.be/Vpg9yizPP_g bla\n"
"[embed]https://youtu.be/Vpg9yizPP_g[/embed]\n"
"[embed] https://youtu.be/Vpg9yizPP_g [/embed]\n"
"[embed]http://youtu.be/Vpg9yizPP_g[/embed]\n"
"[embed] http://youtu.be/Vpg9yizPP_g [/embed]\n"
"<a href src=\"http://youtu.be/Vpg9yizPP_g\">vid</a>\n"
"<a href src=\"http://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g\">vid</a>\n"
"<a href src=\"https://youtube.com/watch?v=Vpg9yizPP_g\">vid</a>")
subst = "\\1\\2\\3\\4"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.IGNORECASE | re.MULTILINE | re.DOTALL)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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