- The first Regular Expression Library on the Web!

Please support RegExLib Sponsors


Regular Expression Details

Title Test Find Pattern Title
*CORRECTED: Again thanks for all the comments below. If you want to include internal domain as well change the partial code (\.[\w-_]+)+ to (\.[\w-_]+)? See the comments below* This is the regular expression I use to add links in my email program. It also ignores those suppose-to-be commas/periods/colons at the end of the URL, like this sentence "check out" (the period will be ignored) Note that it requires some modification to match ones that dont start with http.
Matches |
Author Rating: The rating for this expression. M H
Your Rating
Bad Good

Enter New Comment

Spammers suck - we apologize. Please enter the text shown below to enable your comment (not case sensitive - try as many times as you need to if the first ones are too hard):

Existing User Comments

Title: Problems with "Umlaute"
Name: LEXO
Date: 12/3/2013 3:29:23 AM
I see there's a problem when the URL contains Umlaute like ä, ö or ü. In the past this was not an issue but in a modern CMS like Wordpress and UTF-8 charsets users are allowed to upload filenames containing Umlaute. Thus I slightly modified the Regex to: (http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:\/~\+#äöüÄÖÜ]*[\w\-\@?^=%&\/~\+#])?

Title: matches http://www.textlink
Name: czechmate1976
Date: 8/2/2013 10:55:47 AM
the pattern matches domain name without the top level domain (e.g .com).

Title: Why &
Name: BurninLeo
Date: 12/27/2012 6:29:18 AM
Thanks for doing improvements on the expression even after years! I do not fully understand th & in the expression. To my best knowledge, Regex does not work with HTML entities and [] does only work on single character base - therefore a, m, and p shall already be included in \w, shouldn't they?! @Abdel Hady: When your pattern delimiter is slash (/), please use a blackslash before each slash in the pattern. And when using PHP, of course, every backslash needs another backslash to escape for PHP.

Title: not working with php's preg_replace_callback
Name: Abdel Hady
Date: 4/28/2012 7:44:22 PM
it gives me Fatal error: Uncaught exception 'Mysite_Exception_Warning' with message 'preg_replace_callback(): Unknown modifier '~''

Title: Say good
Name: Hunglt (VietNam)
Date: 2/5/2012 8:27:21 PM
Thanks you

Title: Didn't run in Javascript
Name: Gk
Date: 9/21/2010 11:23:37 AM
IMHO it should be: (http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?

Title: Wrong classes
Name: OD
Date: 5/4/2010 10:22:48 AM
Also, the allowed characters, as defined by RFC 2141, are exactly the class [0-9a-zA-Z()+,.:[email protected];$_!*'%/?#-]

Title: Poor syntax
Name: OD
Date: 5/4/2010 10:15:40 AM
Because of unnecessary escaping in the character classes, '\' is being included and it's not clear whether it should be. [\w\-_] is better written as [\w-] [\w\-\.,@?^=%&:/~\+#] is better written as [\w.,@?^=%&:/~+#-] etc.

Title: Improvements?
Name: Brad
Date: 9/11/2009 10:39:08 AM
Great expression; check these out:

Title: this matches when it shouldnt
Name: Andy
Date: 3/28/2009 3:35:38 PM
I just tried the following PHP and it passes, when it should fail. Any tips? function testURL() { $urltocheck = ''; if(preg_match("/^((http|ftp|https):\/\/|www\.)[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?/", $urltocheck)) { echo "Pass"; return 1; } print "invalid"; return 0;

Title: Displayed wrong
Name: Regex Newbie
Date: 8/3/2008 3:24:20 PM
It works good for me in VisualBasic 6, however it is displayed here wrong, it has been html encoded with &amp; instead of just & Now all I need is a regex that can get relative links out of an <a> tag for my spider.

Title: little mod
Name: Pawel Gruszecki
Date: 3/11/2008 7:37:23 PM
^((http|ftp|https):\/\/|www\.)[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])? I've made a little correction in RegExp posted by John Brooking on 11/7/2007 2:51:56 AM cause php server returned an arror in expression. This works perfect. Matches: Non-Matches: http// hppt://

Title: little mod
Name: Pawel Gruszecki
Date: 3/11/2008 7:37:03 PM
^((http|ftp|https):\/\/|www\.)[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])? I've made a little correction in RegExp posted by John Brooking on 11/7/2007 2:51:56 AM cause php server returned an arror in expression. This works perfect. Matches: Non-Matches: http// hppt://

Title: @ character
Name: Bob Hurt
Date: 1/4/2008 2:14:37 PM
The '@' character doesn't have a special regular expression meaning, does it? If it does, what is the meaning? If it does not, why is the second '@' escaped with a backslash?

Title: Pattern Title - M H
Name: Candida
Date: 11/7/2007 2:51:56 AM
Hi, this was really helpful....thanks for the post. :)

Title: To match
Name: John Brooking
Date: 10/12/2005 5:25:40 PM
First, I've got to say that comments like "This expression does not work" are not helpful. I was able to get it to properly pick up the URL out of the string "The URL is", and it even excluded the final period but got the others. What expression did you get it fail on? Maybe it can be fixed. Be a little helpful! Anyway, I'm mainly posting to say that the following variation ((http|ftp|https):\/\/|www\.)[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])? seems to pick up the ones without the leading protocol string, as long as they start with "www.". Sure, they don't all start with www., but you gotta recognize it somehow. Maybe you could do more with looking for the final top-level string (com, org, us, uk, ...) and backwards from there. Anyhow, this works for my purposes, so I thought I'd share it. It *does* match "".

Title: Mr.
Name: Jon
Date: 9/17/2005 3:28:50 AM
This expression does not work

Title: Span Multiple lines but not match whitespace
Name: MDR
Date: 5/9/2005 11:47:34 AM
How could I make this span multiple lines but not match white spaces. For example acestogo/default.aspx

Title: bad
Name: guili
Date: 2/1/2005 4:01:08 AM
* Allows spaces in URLs * Allows more than one ? in URL

Title: expression does work
Name: akula
Date: 9/24/2004 6:37:35 AM
Hi the below given http link does not match with the expression

Title: Fix for
Name: Venata
Date: 9/16/2004 5:15:07 AM
YOu need to replace [\w]+ with [\w\-]+

Title: Not match
Name: Venata
Date: 9/16/2004 5:11:33 AM
It does not match

Title: good for extracting urls
Name: maximilla
Date: 7/12/2004 4:16:53 PM
gorgeous, thank you! works perfect for my screenscraper.

Title: ops
Name: M H
Date: 4/28/2004 4:09:21 PM
Actually u should replace this partial code (\.[\w]+)+ to (\.[\w]+)?

Title: Depends on what u consider is "wrong"
Name: M H
Date: 4/28/2004 4:02:54 PM
You could have internal URL like http://localhost/blahblahblah.html This is not a code to validate and verify the URL (e.g. you could put http://123.123/ and still get it work, but whether it's a "right" URL is beyond what this code is suppose to do) Anyway I've changed the code to "exclude" the one-part domain for general use but if you want to include internal URLs as well just remove the "{1,}" from the code.

Title: Not great
Name: Stephen
Date: 4/28/2004 3:30:56 PM
Critic is right; doesn't require periods in the domain name. ie, 'http://www' passed

Title: Worked out swell
Name: TjoekBezoer
Date: 3/30/2004 8:40:33 PM
Worked out perfect for me. Used for for checking a 404 paramter in IIS, and it did exactly what it needed to do.

Title: very bad don't use it
Name: critic
Date: 12/1/2003 6:49:39 AM
it allows http://www. for example...

Copyright © 2001-2021, | ASP.NET Tutorials