Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm looking for a regular expression which can extract http URIs from a file (arbitrary text). Note that text can have word "http" but that word might or might not represent a valid URI address.

UPDATE: I understand that HTML is a context free grammar while RegEx is a regular expression but are URIs are just patterns (regular expression). Or not?

share|improve this question
3  
I would just like to point out, that using regex when they are not appropriate may cause madness. What you are asking for is not possible with 100% accuracy. –  Zoredache Jun 7 '11 at 0:08
    
I understand that HTML is a context free grammar while RegEx is a regular expression but are URIs are just patterns (regular expression). Or not? –  Seno Jun 7 '11 at 2:16
1  
@Zoredache, what are you crazy ​‽ You don’t do regex for fun? –  Synetech Jun 7 '11 at 3:01
1  
Provided that you're constraining yourself to well-formed URLs, including protocol prefixes (preferably constrained to a specific set of protocols), regexes can do a pretty good job with this (and many sites use them for just that). But 100% accuracy is still not possible because of things like "Found this at http://somesite.com/foo.", where neither regex nor context-free grammars can reliably determine whether the final "." should be part of the URL or not and the link is unlikely to work if the parser gets it wrong. –  Dave Sherohman Jun 7 '11 at 8:46
add comment

2 Answers

It's harder than you think and no regular-expression is lightly to catch them all.
Consider urls like this

http://www.google.com/search?q=good+url+regex&rls=com.microsoft:*&ie=UTF-8&oe=UTF-8&startIndex=&startPage=1

ftp://joe:password@ftp.filetransferprotocal.com

google.ru

https://some-url.com?query=&name=joe?filter=.#some_anchor

Here is a good short article on the subject A good url regular expression?

^(?#Protocol)(?:(?:ht|f)tp(?:s?)\:\/\/|~\/|\/)?(?#Username:Password)  
(?:\w+:\w+@)?(?#Subdomains)(?:(?:[-\w]+\.)+(?#TopLevel Domains)  
(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|travel|  
[a-z]{2}))(?#Port)(?::[\d]{1,5})?(?#Directories)(?:(?:(?:\/(?:[-\w~!$+|.,=]  
|%[a-f\d]{2})+)+|\/)+|\?|#)?(?#Query)(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])  
+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?  
(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?#Anchor)(?:#(?:[-\w~!$+|.,*:=]  
|%[a-f\d]{2})*)?$

Here is another a bit shorter from An Improved Liberal, Accurate Regex Pattern for Matching URLs

(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.]  
[a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+  
(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))
share|improve this answer
    
I like it how SuperUsers (regex-based) highlighting/autolinking fails your examples how bad regex are for finding links :) –  zhenech Apr 5 at 23:08
add comment

Maybe something like this:

(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.