Alex Rivera | Logout

Regular Expression to match both relative and absolute URLs

Asked 2009-06-15T22:20:33.937
9

Anyone want to try their hand at coming up with a regex that matches both:

I think it might be impossible to do it with one regex, but you never know.

EDIT: To clarify, what I'm trying to do is pick out all URI's from a document (Not a HTML document).

Edit
Report

1 Answer

0

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).

answered 2009-06-15T22:53:51.827

Your Answer