我正在尝试从文件中解析 URL。我的正则表达式有 80% 的时间都在工作,但我需要针对异常进行修改。它开始变得复杂了,我想知道如何为这个输入文件编写一个漂亮而干净的正则表达式,以便在一秒钟内获得一个组中的主机和 URI 部分。
例如:主机http://stackoverflow.com/index.php
在哪里,是 URI。stackoverflow.com
/index.php
输入文件 :
//cdn.sstatic.net/stackoverflow/img/favicon.ico
//cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png
/opensearch.xml
/
#
http://www.stackoverflow.com
http://www.stackoverflow.com/
http://stackoverflow.com/
http://careers.stackoverflow.com
aaa#aaa.com
aaa.com#aaa
aaa#aaa
#aaa
#
fakedomain/index.php
fakedomain.com/index.php
fakedomain.com/
/fakedomain.com/
/index.html/
index.html
正则表达式:
(?:.*?//)?(.*?)(/.*|$)
结果 :
1 : //cdn.sstatic.net/stackoverflow/img/favicon.ico has 2 groups:
cdn.sstatic.net
/stackoverflow/img/favicon.ico
2 : //cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png has 2 groups:
cdn.sstatic.net
/stackoverflow/img/apple-touch-icon.png
3 : /opensearch.xml has 2 groups:
/opensearch.xml
4 : / has 2 groups:
/
5 : http://www.stackoverflow.com has 2 groups:
http:
//www.stackoverflow.com
6 : http://www.stackoverflow.com/ has 2 groups:
www.stackoverflow.com
/
7 : http://stackoverflow.com/ has 2 groups:
stackoverflow.com
/
8 : http://careers.stackoverflow.com has 2 groups:
http:
//careers.stackoverflow.com
7 : fakedomain/index.php has 2 groups:
fakedomain
/index.php
8 : fakedomain.com/index.php has 2 groups:
fakedomain.com
/index.php
9 : fakedomain.com/ has 2 groups:
fakedomain.com
/
10 : /fakedomain.com/ has 2 groups:
/fakedomain.com/
11 : /index.html/ has 2 groups:
/index.html/
12 : index.html has 2 groups:
index.html
13 : has 2 groups:
C# 正则表达式测试器: http: //derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx
那么我怎样才能删除.ico
或.png
添加一些其他修复程序的链接并获得一个漂亮而干净的正则表达式呢?