解析变量URI(RegEx,Uri,String-Functions?)c#

时间:2012-08-20 11:38:26

标签: c# regex string parsing url

我正在编写RTSP客户端模块,为此,我需要解析一个非常可变的URI。但我完全坚持我应该使用哪种方法(最安全的)以及如何实现这一目标。

示例URI可能如下所示:

rtsp://192.168.1.100:554/videocam/media/1/video/1
\_/    \_______________/\_______/\______________/
 |              |           |           |
scheme      authority     [sub]     [mediacontrol]

但也有其他可能性:

192.168.1.100/videocam/media/1/video/1
192.168.1.100:6000/media/1/video/1
192.168.1.100:6000/videocam

我需要以下信息:

IP         | how can I recognise this pattern [num].[num].[num].[num]?
Port       | easy if the string contains rtsp://, but what about just a number? 1-65555
Sub        | Optional subpath, can completely vary!
MediaLevel | Optional MediaLevel (indicator for stream/track), 
             not to be confused with the path. MediaLevel can be also just like this: track1 or m1s3 or media1/video1.. see?
             I can't go for the slash, also the path also can contain multiple slashes

也许有这样的任务库?

谢谢。

3 个答案:

答案 0 :(得分:2)

var uri = new Uri("rtsp://192.168.1.100:554/videocam/media/1/video/1");
var host = uri.Host;
var port = uri.Port;
var sub = uri.Segments[1];
var mlevel = uri.Segments.Skip(2).ToArray();

答案 1 :(得分:0)

以下是如何使用UriBuilder类的快速示例。它有点冗长,因为它是一个例子,还没有准备好生产。如果要识别更多的潜艇,则可以将它们添加到子列表中,如示例所示。

class Program
    {
        private static string _scheme = string.Empty;
        private static string _host = string.Empty;
        private static string _sub = string.Empty;

        static void Main(string[] args)
        {
            ParseUri("rtsp://192.168.1.100:554/videocam/media/1/video/1");
            ParseUri("192.168.1.100/videocam/media/1/video/1");
            ParseUri("192.168.1.100:6000/media/1/video/1");
            ParseUri("192.168.1.100:6000/videocam");
            // example of adding a new sub
            Sub.Add("sample");
            ParseUri("192.168.1.100:6000/sample/");
            Console.ReadLine();
        }

        private static void ParseUri(string URI)
        {
            UriBuilder uri = new UriBuilder(URI);
            _scheme = uri.Scheme;
            _host = uri.Host;
            _sub = string.Empty;
            StringBuilder sb = new StringBuilder();
            foreach (string s in uri.Uri.Segments)
            {
                if (Sub.Contains(s.Replace("/","")))
                {_sub = s;}
                else
                { sb.Append(s); }
            }

            Console.Out.WriteLine("+++++++++++++");
            Console.Out.WriteLine("URI: {0}",URI);
            Console.Out.WriteLine("Scheme: {0}", _scheme);
            Console.Out.WriteLine("sub: {0}",_sub);
            Console.Out.WriteLine("mediaControl: {0}", sb.ToString());
        }

        private static List<string> Sub
        {
            get
            {
                List<string> sub = new List<string>();
                sub.Add("videocam");
                return sub;
            }
        }
    }

答案 2 :(得分:0)

                trace("Url      : {0}", turl.Text);
                var uri = new Uri(turl.Text);
                string host = uri.Host;
                int port = uri.Port;
                string userInfo = uri.UserInfo;
                string subStream = "";
                string userName = "";
                string password = "";
                if (uri.Segments?.Any() == true)
                {
                    subStream = string.Join("", uri.Segments);
                }
                if (!string.IsNullOrEmpty(userInfo))
                {
                    if (userInfo.Contains(":"))
                    {
                        string[] data = userInfo.Split(':');
                        userName = data[0];
                        password = data[1];
                    }
                    else
                    {
                        userName = userInfo;

                    }
                }
                trace("host     : {0}", host);
                trace("port     : {0}", port);
                trace("userName : {0}", userName);
                trace("password : {0}", password);
                trace("subStream: {0}", subStream);

Without user info enter image description here