如何将一个URL地址,用正则表达式分为四段?谢谢。 本帖最后由 wtoeb 于 2012-02-12 21:35:53 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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]); 我是这样写的,请帮我修改一下,谢谢。http://(?<Host>[^\:]*)(?:\:)(?<Port>\d+)(.*)/([^/]+)$ “/”个数>=4时,用:http://(.+?):(\d+)/(.*)/([^/]+)$“/”个数<=3时,用:http://(.+?):(\d+)/([^/]+)$ 楼主对正则已经很有了解,就不多解释了。^http\:\/\/(?<Host>[^\:]*):(?<Port>\d)*\/(?<Path>.*)\/(?<File>[^\/]*)$ 刚才写的有点问题,在加命名的时候把端口的*号放到括号外面去了,不好意思。更正答案:^http\:\/\/(?<Host>[^\:]*):(?<Port>\d*)\/(?<Path>.*)\/(?<File>[^\/]*)$ ^http://(?<Host>[^\:]*)(?:\:)(?:(?<Port>\d+)/)(?<Path>.*?)(?:/?(?<Para>[^/]+[._-]?)\.(?<UriExt>aspx|shtml|shtm|html|htm))?$ 修改了一下,可以适用于无端口和无路径的情况。^http\:\/\/(?<Host>[^\:\/]*)\:?(?<Port>\d*)(?<Path>\/?.*)\/(?<File>[^\/]*)$个人不太建议对文件名进行匹配,因为这会把情况复杂化。你完全在匹配结束后,用“.”split分割“File”变量,就可以得到文件名与后缀了。关于你反查不成功的问题,主要在于/后面那个?,那个?表示这个/可以没有。如果你想把它做为分界符,那么这个符号是一定要存在的。 用(),就分段了。然后在Group[]里面取值。正则的基础知识 string str = @"http://dev-super:82/node23/public_index_site12_org12_n12_i3_p3.htmlhttp://dev-super:82/news/node23/public_index_site12_org12_group13_n12_i3_view12_p3.htmlhttp://dev-super:82/node23/news/public_index_site12_org12_n12_i3_p3.htmlhttp://dev-super:82/news/node23/node21/index.htmlhttp://dev-super:82/node23/news/index.htmlhttp://dev-super:82/node23/index.htmlhttp://dev-super:82/index_p3.htmlhttp://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.htmldev-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*/ 我的textBox怎么不能绑定? 请教大文件的读取与比较 关于Excel表格数据导入到Access数据库表中的问题 如何让分布视图中的DIV内容显示在最上层 请问一个ado.net的问题 虚心请教:“在线试听" m3u Stream 急求解答 如何获取接口里的实现方法WorksheetFunction,求大神帮助啊 请教各位大大。 请用C#编写一个冒泡排序算法,立即给分! 菜鸟求助 跪求.NET相关书籍文档
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]);
http://(?<Host>[^\:]*)(?:\:)(?<Port>\d+)(.*)/([^/]+)$
“/”个数<=3时,用:http://(.+?):(\d+)/([^/]+)$
楼主对正则已经很有了解,就不多解释了。^http\:\/\/(?<Host>[^\:]*):(?<Port>\d)*\/(?<Path>.*)\/(?<File>[^\/]*)$
修改了一下,可以适用于无端口和无路径的情况。^http\:\/\/(?<Host>[^\:\/]*)\:?(?<Port>\d*)(?<Path>\/?.*)\/(?<File>[^\/]*)$个人不太建议对文件名进行匹配,因为这会把情况复杂化。你完全在匹配结束后,用“.”split分割“File”变量,就可以得到文件名与后缀了。关于你反查不成功的问题,主要在于/后面那个?,那个?表示这个/可以没有。如果你想把它做为分界符,那么这个符号是一定要存在的。
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
*/