怎么将一个URL地址,用正则表达式分为四段?多谢
如何将一个URL地址,用正则表达式分为四段?谢谢。
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
分为:
------解决方案--------------------
URL解析直接用.net framework自带的Uri类来解析就行了
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
分为:
- SQL code
第一段 第二段 第三段 第四段 dev-super 82 node23 public_index_site12_org12_n12_i3_p3.html dev-super 82 news/node23 public_index_site12_org12_group13_n12_i3_view12_p3.html … dev-super 82 index_p3.html …
------解决方案--------------------
URL解析直接用.net framework自带的Uri类来解析就行了
- C# code
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]);
------解决方案--------------------
“/”个数>=4时,用:http://(.+?):(\d+)/(.*)/([^/]+)$
“/”个数<=3时,用:http://(.+?):(\d+)/([^/]+)$
------解决方案--------------------
------解决方案--------------------
楼主对正则已经很有了解,就不多解释了。
^http\:\/\/(?<Host>[^\:]*):(?<Port>\d)*\/(?<Path>.*)\/(?<File>[^\/]*)$
------解决方案--------------------
刚才写的有点问题,在加命名的时候把端口的*号放到括号外面去了,不好意思。
更正答案:^http\:\/\/(?<Host>[^\:]*):(?<Port>\d*)\/(?<Path>.*)\/(?<File>[^\/]*)$
------解决方案--------------------
修改了一下,可以适用于无端口和无路径的情况。
^http\:\/\/(?<Host>[^\:\/]*)\:?(?<Port>\d*)(?<Path>\/?.*)\/(?<File>[^\/]*)$
个人不太建议对文件名进行匹配,因为这会把情况复杂化。
你完全在匹配结束后,用“.”split分割“File”变量,就可以得到文件名与后缀了。
关于你反查不成功的问题,主要在于/后面那个?,那个?表示这个/可以没有。
如果你想把它做为分界符,那么这个符号是一定要存在的。
------解决方案--------------------
- C# code
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.html dev-super 82 news/node23/ public_index_site12_org12_group13_n12_i3_view12_p3.htm l dev-super 82 node23/news/ public_index_site12_org12_n12_i3_p3.html dev-super 82 news/node23/node21/ index.html dev-super 82 node23/news/ index.html dev-super 82 node23/ index.html dev-super 82 空 index_p3.html dev-super 82 空 public_index123_site12_org12_group13_n12_i3_view12_p3.html */