Hey guys. I', trying to have a regex made up to match all URL's like these:
- http://example.com
- http://example.com/
- http://example.com/index.html
- http://example.com/index
- http://example.com/index/
- http://www.example.com
- http://www.example.com/
- http://www.example.com/index.html
- http://www.example.com/index
- http://www.example.com/index/
and to match URLs that have ' # ' or ' ? ' only until the character before those 2 ones. This way http://example.com/index.php?p=Hey -> http://example.com/index.php
The regex code I have so far works well when selecting only certain file types or a folder except one case:
- if I have an URL that does not end with either a file extension (eg: .html, .php) or a folder (eg: /) the pattern will not match properly as some URLs (eg: http://example.com/about-me) will be excluded.
Any help is appreciated. Thanks everyone.
This is the regex:
^(?<protocol>http(s?))://(?<domain>[^/\r\n#?]+)(?<path>/[^?#]*(?:html|开发者_运维知识库php|/))?
Not sure what language you're using, but regular expressions may not be necessary for this if you've got a list of URLs already.
In C#, you could do something like this:
string a = "http://example.com/index.php?p=Hey";
string b = a.Remove(a.IndexOfAny(new char[] {'?', '#'}, 0));
This might do what you want:
^(?<protocol>http(s?))://(?<domain>[^/\s#?]+)(?<path>/[^\s#?]*)?(?<query>.*)?
The query will contain the rest that you might want to ignore.
精彩评论