views:

196

answers:

3

I need to test for general URLs using any protocol (http, https, shttp, ftp, svn, mysql and things I don't know about).

My first pass is this:

\w+://(\w+\.)+[\w+](/[\w]+)(\?[-A-Z0-9+&@#/%=~_|!:,.;]*)?

(PCRE and .NET so nothing to fancy)

+1  A: 

adding that RegEx as a wiki answer:

[\w+-]+://([a-zA-Z0-9]+\.)+[[a-zA-Z0-9]+](/[%\w]+)(\?[-A-Z0-9+&@#/%=~_|!:,.;]*)?

option 2 (Re CMS)

^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?

But that's to lax for anything sane so trimmed to make it more restrictive and to differentiate off other things.

proto      ://  name      : pass      @  server    :port      /path     ? args
^([^:/?#]+)://(([^/?#@:]+(:[^/?#@:]+)?@)?[^/?#@:]+(:[0-9]+)?)(/[^?#]*)(\?([^#]*))?
BCS
Another possibility is 'svn+ssh://'. I don't think \w would match '+'.
brass-kazoo
You are correct, at least in the context of PCRE.
Dave Sherohman
+1  A: 

Similar questions:

only to mention those I saw in the first page of URL tag...

PhiLho
BCS
+2  A: 

According to RFC2396:

^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
CMS
OK so the windows //server/dir/file thing IS a URL? kind of make the file:////server/dir/file in FF/IE look even more bazaar
BCS