Anyone want to try their hand at coming up with a regex that matches both:
I
(
((http|https|ftp)://([\w-\d]+\.)+[\w-\d]+){0,1} // Capture domain names or IP addresses
(/[\w~,;\-\./?%&+#=]*) // Capture paths, including relative
)
Rationale for this answer:
Caveats:
Edit: whoops, fixed closing paren problem.
I used naming capture groups. We get better matches when the scheme is present. Like www.foo.com/bar would only match /bar.
(?:
(?:(?<scheme>https?|file)://)
(?<host>[^/]+)
(?<path>/(?:[^\s])+)?
)
|
(?<path>/(?:[^\s])+)
This is what you could do for javascript
var result = text.match(/(?:(?:(https?|file):\/\/)([^\/]+)(\/(?:[^\s])+)?)|(\/(?:[^\s])+)/g);
Test data
sadfasdf /foo/bar/ba090z.gif asdfasdf /foo/bar/ sadfasdf asdflkj; http://www.foo.com/foo/bar some stuff http://user:pw@www.foo.com:80/r?stuff%20stuff
user:pw@www.foo.com:80/r?stuff%20stuff
(http:/)?(/[\w.]+)+/?
matches these, but maybe you had stricter conditions in mind?
(http:\/)?(\/[\w\.\-]+)+\/?
Similar to Alex's.
That's a tricky one because there are so many valid characters in URL's (before they get url encoded).
Here's my shot:
(http:/|https:/)?(/[^\s"'<>]+)+/?
Also similar to Alex's. The only problem I found with Alex's is that it wouldn't match things like pound signs, dashes, stuff like that. Whereas mine will match all of that.
EDIT -- In fact the only thing that keeps it from being too greedy is the instruction to NOT match whitespace, quotes, apostrophes, or chevrons.
Not easy and you maybe end up having "too much URI" catched, however what about:
((http://|https://)([^/])+)*(/([^\s])*(/))(((\w)*\.[\w]{3,10})|(\w+))?
Basically you have a couple of groups there. On defining the protocol. One is looking for the directory and one is looking for a file at the end. But! this approach is very limited. If you need a real URI validation and! separation (port, username, password, filter out unwanted characters!) you will probably end up with a way more complex expression. Good luck!
Update:
You didn't asked for this, however for those guys coming from search engines wanting to learn more about regex I would like to plug this free program I used for this attempt "The Regex Coach" (Nope, not affiliated).