C#正则截取URL网址字符串

string str = "http://www.rczjp.cn/A/B/C/index.aspx?cid=11&sid=22";
 

            Regex reg = new Regex(@"(?imn)(?<do>http://[^/]+/)(?<dir>([^/]+/)*([^/.]*$)?)((?<page>[^?.]+\.[^?]+)\?)?(?<par>.*$)");
            MatchCollection mc = reg.Matches(str);
            foreach (Match m in mc)
            {
                Console.WriteLine(m.Groups["do"].Value);  //http://www.rczjp.cn/
                Console.WriteLine(m.Groups["dir"].Value); //A/B/C/
                Console.WriteLine(m.Groups["page"].Value);  //index.aspx
                Console.WriteLine(m.Groups["par"].Value); //cid=11&sid=22
            }
            Console.Read();

 

posted on 2012-05-18 10:31  HOT SUMMER  阅读(1875)  评论(0编辑  收藏  举报

导航