Change youtube url regex to match youtube urls more formats
diff --git a/lib/streamlit/elements/media.py b/lib/streamlit/elements/media.py
index e24082bf4392..d20618ba9ffd 100644
--- a/lib/streamlit/elements/media.py
+++ b/lib/streamlit/elements/media.py
@@ -192,16 +192,10 @@ def dg(self) -> DeltaGenerator:
         return cast("DeltaGenerator", self)
 
 
-# Regular expression explained at https://regexr.com/4n2l2 Covers any youtube
-# URL (incl. shortlinks and embed links) and extracts its code.
-YOUTUBE_RE: Final = re.compile(
-    # Protocol
-    r"http(?:s?):\/\/"
-    # Domain
-    r"(?:www\.)?youtu(?:be\.com|\.be)\/"
-    # Path and query string
-    r"(?P<watch>(watch\?v=)|embed\/)?(?P<code>[\w\-\_]*)(&(amp;)?[\w\?=]*)?"
-)
+# Regular expression from
+# https://gist.github.com/rodrigoborgesdeoliveira/987683cfbfcc8d800192da1e73adc486?permalink_comment_id=4645864#gistcomment-4645864
+# Covers any youtube URL (incl. shortlinks and embed links) and extracts its video code.
+YOUTUBE_RE: Final = r"^((https?://(?:www\.)?(?:m\.)?youtube\.com))/((?:oembed\?url=https?%3A//(?:www\.)youtube.com/watch\?(?:v%3D)(?P<video_id_1>[\w\-]{10,20})&format=json)|(?:attribution_link\?a=.*watch(?:%3Fv%3D|%3Fv%3D)(?P<video_id_2>[\w\-]{10,20}))(?:%26feature.*))|(https?:)?(\/\/)?((www\.|m\.)?youtube(-nocookie)?\.com\/((watch)?\?(app=desktop&)?(feature=\w*&)?v=|embed\/|v\/|e\/)|youtu\.be\/)(?P<video_id_3>[\w\-]{10,20})"
 
 
 def _reshape_youtube_url(url: str) -> str | None:
@@ -221,9 +215,14 @@ def _reshape_youtube_url(url: str) -> str | None:
     .. output::
         https://www.youtube.com/embed/_T8LGqJtuGc
     """
-    match = YOUTUBE_RE.match(url)
+    match = re.match(YOUTUBE_RE, url)
     if match:
-        return "https://www.youtube.com/embed/{code}".format(**match.groupdict())
+        code = (
+            match.group("video_id_1")
+            or match.group("video_id_2")
+            or match.group("video_id_3")
+        )
+        return f"https://www.youtube.com/embed/{code}"
     return None
 
 
diff --git a/lib/tests/streamlit/elements/video_test.py b/lib/tests/streamlit/elements/video_test.py
index e80359b52ed9..a7ca65f780ec 100644
--- a/lib/tests/streamlit/elements/video_test.py
+++ b/lib/tests/streamlit/elements/video_test.py
@@ -59,11 +59,20 @@ def test_youtube_urls_transformed_to_embed_links(self):
             "https://youtu.be/_T8LGqJtuGc",
             "https://www.youtube.com/watch?v=kmfC-i9WgH0",
             "https://www.youtube.com/embed/sSn4e1lLVpA",
+            "https://youtube.com/e/0TSXM-BGqHU",
+            "https://youtube.com/v/OIQskkX_DK0",
+            # HTTP should also work correctly
+            "http://youtu.be/4sPnOqeUDmk",
+            "http://www.youtube.com/embed/92jUAXBmZyU",
         )
         yt_embeds = (
             "https://www.youtube.com/embed/_T8LGqJtuGc",
             "https://www.youtube.com/embed/kmfC-i9WgH0",
             "https://www.youtube.com/embed/sSn4e1lLVpA",
+            "https://www.youtube.com/embed/0TSXM-BGqHU",
+            "https://www.youtube.com/embed/OIQskkX_DK0",
+            "https://www.youtube.com/embed/4sPnOqeUDmk",
+            "https://www.youtube.com/embed/92jUAXBmZyU",
         )
         # url should be transformed into an embed link (or left alone).
         for x in range(0, len(yt_urls)):

<!--
⚠️ BEFORE CONTRIBUTING PLEASE READ OUR CONTRIBUTING GUIDELINES!
https://github.com/streamlit/streamlit/wiki/Contributing
-->

## Describe your changes

Previously our YouTube regex doesn't match valid YouTube links (e.g. "https://youtube.com/e/0TSXM-BGqHU" or "https://youtube.com/v/OIQskkX_DK0"), which is why we failed to display videos in Streamlit via `st.video`.

Current Regexp covers those cases too.

Also instead of compiling regex on import time we do that lazily on demand (for a first run performance this should affect positively, and for consequent reruns this should not affect negatively, because [Python caches regex patterns](https://docs.python.org/3/howto/regex.html#module-level-functions).



## GitHub Issue Link (if applicable)

## Testing Plan

- Explanation of why no additional tests are needed
- Unit Tests (JS and/or Python) Added
- E2E Tests
- Any manual testing needed?

---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.
