本帖最后由 wtoeb 于 2012-02-12 21:35:53 编辑

解决方案 »

  1.   

    URL解析直接用.net framework自带的Uri类来解析就行了
                Uri uri = new Uri("http://dev-super:82/news/node23/node21/index.html");
                string[] segments = uri.Segments;
                Console.WriteLine("{0}  {1} {2} {3}", uri.Host, uri.Port, string.Join(string.Empty, segments, 0, segments.Length - 1), segments[segments.Length - 1]);
      

  2.   

    我是这样写的,请帮我修改一下,谢谢。
    http://(?<Host>[^\:]*)(?:\:)(?<Port>\d+)(.*)/([^/]+)$
      

  3.   

    “/”个数>=4时,用:http://(.+?):(\d+)/(.*)/([^/]+)$
    “/”个数<=3时,用:http://(.+?):(\d+)/([^/]+)$
      

  4.   


    楼主对正则已经很有了解,就不多解释了。^http\:\/\/(?<Host>[^\:]*):(?<Port>\d)*\/(?<Path>.*)\/(?<File>[^\/]*)$
      

  5.   

    刚才写的有点问题,在加命名的时候把端口的*号放到括号外面去了,不好意思。更正答案:^http\:\/\/(?<Host>[^\:]*):(?<Port>\d*)\/(?<Path>.*)\/(?<File>[^\/]*)$
      

  6.   

    ^http://(?<Host>[^\:]*)(?:\:)(?:(?<Port>\d+)/)(?<Path>.*?)(?:/?(?<Para>[^/]+[._-]?)\.(?<UriExt>aspx|shtml|shtm|html|htm))?$
      

  7.   


    修改了一下,可以适用于无端口和无路径的情况。^http\:\/\/(?<Host>[^\:\/]*)\:?(?<Port>\d*)(?<Path>\/?.*)\/(?<File>[^\/]*)$个人不太建议对文件名进行匹配,因为这会把情况复杂化。你完全在匹配结束后,用“.”split分割“File”变量,就可以得到文件名与后缀了。关于你反查不成功的问题,主要在于/后面那个?,那个?表示这个/可以没有。如果你想把它做为分界符,那么这个符号是一定要存在的。
      

  8.   

    用(),就分段了。然后在Group[]里面取值。正则的基础知识
      

  9.   

                string str = @"http://dev-super:82/node23/public_index_site12_org12_n12_i3_p3.html
    http://dev-super:82/news/node23/public_index_site12_org12_group13_n12_i3_view12_p3.html
    http://dev-super:82/node23/news/public_index_site12_org12_n12_i3_p3.html
    http://dev-super:82/news/node23/node21/index.html
    http://dev-super:82/node23/news/index.html
    http://dev-super:82/node23/index.html
    http://dev-super:82/index_p3.html
    http://dev-super:82/public_index123_site12_org12_group13_n12_i3_view12_p3.html";
                Regex reg = new Regex(@"(?is)http://([^:]+):([^/]+)/([^.]*/)?(.*?html)");
                foreach (Match m in reg.Matches(str))
                    Console.WriteLine("{0} {1} {2} {3}\r\n", m.Groups[1].Value, m.Groups[2].Value, m.Groups[3].Value.Length==0?"空":m.Groups[3].Value, m.Groups[4].Value);
    /*
    dev-super 82 node23/ public_index_site12_org12_n12_i3_p3.htmldev-super 82 news/node23/ public_index_site12_org12_group13_n12_i3_view12_p3.htm
    ldev-super 82 node23/news/ public_index_site12_org12_n12_i3_p3.htmldev-super 82 news/node23/node21/ index.htmldev-super 82 node23/news/ index.htmldev-super 82 node23/ index.htmldev-super 82 空 index_p3.htmldev-super 82 空 public_index123_site12_org12_group13_n12_i3_view12_p3.html
    */