直播流URL协议验证的正则表达式需求(含未知协议支持)
Alright, let's break down how to create a regex that works for live stream URLs when you can't predict the exact protocol upfront. Live streaming uses a ton of different protocols (rtsp, rtmp, srt, hls, etc.), so our solution needs to be flexible but still reliable.
Core Approach
The key requirements here are:
- Ensure the URL starts with a valid protocol followed by
://(the standard URI protocol separator) - Allow for common protocol naming rules (letters, numbers, hyphens)
- Accept any valid URL structure after the protocol (since live stream URLs can vary wildly—IP addresses, domains, ports, paths, query params, etc.)
Basic Regex (Flexible for Any Protocol)
This version will match any URL that follows the protocol://... format, regardless of the specific protocol:
^[a-zA-Z0-9-]+://.+$
Let's break down each part:
^: Anchors the match to the start of the string, so we know the protocol is the first thing in the URL[a-zA-Z0-9-]+: Matches the protocol name—supports uppercase/lowercase letters, numbers, and hyphens (covers every standard streaming protocol and most custom ones)://: The mandatory separator that defines a protocol-based URL.+: Matches everything after the separator (as long as it's not empty, ensuring the URL has a valid body)$: Anchors the match to the end of the string, preventing extra invalid characters at the end
Precise Regex (Only Known Streaming Protocols)
If you want to restrict matches to only common live streaming protocols (to avoid matching unrelated protocols like ftp:// or file://), use this enumerated version:
^(rtsp|rtmp|rtmps|http|https|srt|hls|webrtc|udp|tcp)://.+$
You can add or remove protocols from the pipe-separated list based on your specific use case—this covers the vast majority of live streaming scenarios.
Test Cases
Let's see what this matches and rejects:
- Valid matches (both regex versions will catch these):
rtsp://192.168.1.100:554/camera/streamrtmp://live.example.com/broadcast/streamKeyhttps://cdn.example.com/hls/live/stream.m3u8srt://srt-server.example:9999?streamid=live/channel01
- Invalid matches:
192.168.1.100:554/stream(no protocol prefix)ftp://files.example.com/old-video.mp4(non-streaming protocol, rejected by the precise regex)rtsp://(protocol only, no URL body)
Key Notes
- If you need stricter validation for the URL body (like checking valid domain formats or port ranges), you can replace the
.+with a more specific regex. But be careful—live stream URLs often have unique structures (like SRT's query params or RTMP's stream keys), so over-restricting might flag valid URLs. - Protocol case sensitivity: The basic regex allows uppercase and lowercase protocols (e.g.,
RTSP://orrtsp://). If you need to enforce lowercase, modify the character set to[a-z0-9-]+. - Custom protocols: If you're working with a niche or custom streaming protocol, just add it to the enumerated list in the precise regex, or rely on the flexible basic version if you don't know it upfront.
内容的提问来源于stack exchange,提问作者Jisha Techversant




