Title |
Test
Find
Ultimate URL URN Regex
|
Expression |
^((nntp|sftp|ftp(s)?|http(s)?|gopher|news|file|telnet):\/\/)?(([a-zA-Z0-9\._-]*([a-zA-Z0-9]\.[a-zA-Z0-9])[a-zA-Z]{1,6})|(([0-9]{1,3}\.){3}[0-9]{1,3}))(:\d+)?(\/[^:][^\s]*)?$ |
Description |
Finally, an expression that can reliably match any valid URL/URN! Future proof against vanity TLDs, doesn't require a schema, can parse IPv4s with or without port, can parse regular URLs with/without port, can accept extra baggage at the end, and, with a slight modification (a la ^$ and changing the final rule around a bit) this can be used to find and activate URLs in bodies of text safely and reliably. Have fun (tell me if it could be improved)! |
Matches |
http://www.google.com/#sclient=psy&hl=en&q=this+is+a+google+serach+%5E((Csftp)%3A%2F%2F)%3F((%5Ba-zA-Z0-9%5C._-%5D%2B%5C.%5Ba-zA-Z%5D%7B2%2C7%7D)%7C((%5B0-9%5D%7B1%2C3%7D%5C.)%7B3%7D%5B0-9%5D%7B1%2C3%7D))(%3A%5Cd%2B)%3F(%2F%5Ba-zA-Z0-9%5C%26amp%3B%25_%5C.%2F-~-%23%5D*)%3F%24&aq=f&aqi=&aql=&oq=&gs_rfai=&pbx=1&fp=ca05a7bb65e82229 | http://192.168.1.1:5649 |
Non-Matches |
htttp://google.com | http://google-.com | http://google.123 | http://google.commmmmm | http://123.456.789.1231 |
Author |
Rating:
Xunnamius
|
Source |
My mind |
Your Rating |
|
Title: Lol
Name: Xunnamius
Date: 10/6/2012 4:24:52 PM
Comment:
@ Colin I guess I'll just change the title to "Any URL/URN matcher," thanks.
Nice mod!
Title: Worked (with a lilttle modification)
Name: Francisco Cittadini
Date: 6/14/2012 1:42:32 PM
Comment:
Great, it helped me a lot, thanks! However I modified it like the following so that it doesn't accept 255.255.255.256.
^((nntp|sftp|ftp(s)?|http(s)?|gopher|news|file|telnet):\/\/)?(([a-zA-Z0-9\._-]*([a-zA-Z0-9]\.[a-zA-Z0-9])[a-zA-Z]{1,6})|((([01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}([01]?\d{1,2}|2[0-4]\d|25[0-5])))(:\d+)?(\/[^:][^\s]*)?$
Title: Not quite working
Name: Colin Griffith
Date: 2/3/2012 10:37:20 PM
Comment:
That doesn't quite work. Look at my regex here:
http://regexlib.com/REDetails.aspx?regexp_id=3246
Many of the URIs that are in the official standard URI documentation as examples will not match yours (especially the telephone one), whereas my regular expression I had to test extensively to get it to match them. Also, some of the example non-matches you show here, would actually match as a valid URI. Maybe not valid URLs (no such thing as .commmmmmmm), but it'd be a valid URI.
Title: great
Name: scott
Date: 12/17/2010 3:03:52 AM
Comment:
beautiful, just beautiful. finally. thanks.
Title: YES!
Name: Max
Date: 12/17/2010 3:00:24 AM
Comment:
Signed up just go commend. JUST what I needed! Thanks!