到目前为止,我有以下几点:
^((http[s]?|ftp):\/\/)(([^.:\/\s]*)[\.]([^:\/\s]+))(:([^\/]*))?(((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(\?([^#]*))?(#(.*))?)?$一直在针对这些方面进行测试:
https://www.google.com.ar:8080/dir/1/2/search.html?arg=0-a&arg1=1-b&arg3-c#hash
https://google.com.ar:8080/dir/1/2/search.html?arg=0-a&arg1=1-b&arg3-c#hash
https://google.com:8080/dir/1/2/search.html?arg=0-a&arg1=1-b&arg3-c#hash
http://www.foo.com
http://www.foo.com/
http://blog.foo.com/
http://blog.foo.com.ar/
http://foo.com
http://blog.foo.com
http://foo.com.ar我正在使用以下工具来测试regexes:regex测试仪
到目前为止,我已经能够产生以下几个组:
我将使用这个regex来为我的跨域重定向超链接应用程序更改子域。
使用Request.Url作为参数,我想从
http://example.com或http://www.example.com到http://blog.example.com
我怎样才能做到这一点?
我不知道,如果有的话,当前的子域(比如,nothing,www,blog,或论坛)实际上是什么?
最好的替代方法是什么?
我真正需要的是找出什么是顶级域名。无论是http://www.example.com、http://blog.example.com还是http://example.com,我都想得到example.com。
发布于 2011-07-23 23:59:44
最好的替代方法是什么?
这可能不是你想要的答案..。但是,最好的方法是利用System.Uri类。
Uri类将很容易地为您提取Host -然后您可以打开主机“split”。分隔符-这应该很容易让您访问当前的子域。
这只是我的观点--特别是因为我发现很难维护像^((http[s]?|ftp):\/\/)(([^.:\/\s]*)[\.]([^:\/\s]+))(:([^\/]*))?(((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(\?([^#]*))?(#(.*))?)?$这样的regex代码。
发布于 2011-07-24 00:35:13
您可以使用Uri类来解析字符串。除了分段之外,还有许多可用的属性:
Uri MyUri = new Uri("https://www.google.com.ar:8080/dir/1/2/search.html?arg=0-a&arg1=1-b&arg3-c#hash");
foreach (String Segment in MyUri.Segments)
Response.Write(Segment + "<br />");发布于 2011-07-24 00:53:58
我认为您应该重新考虑在这种情况下是否真的需要使用RegEx;
- Regex-patterns are very error-prone and quite hard to maintain and according to me you won't get any advantage of it. I recommend you to get rid off the Regex. Perhaps the result will be 2 - 3 more lines of code, but it will work, your code will be much better readable and easier to understand.
https://stackoverflow.com/questions/6804036
复制相似问题