# ★ A Liberal, Accurate Regex Pattern for Matching URLs

<http://daringfireball.net/2009/11/liberal_regex_for_matching_urls>

- Host: [daringfireball.net](https://links.thingelstad.xyz/hosts/daringfireball.net/)
- Saved: November 27, 2009 (2009-11-28T05:11:13Z)
- Record: https://links.thingelstad.xyz/links/9c3505d50557/
- JSON: https://links.thingelstad.xyz/links/9c3505d50557.json

## From the page (probably an excerpt, not Jamie's words)

A common programming problem: identify the URLs in an arbitrary string of text, where by “arbitrary” let’s agree we mean something unstructured such as an email message or a tweet. I offer a solution, in the form of the following regex pattern:


\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))


This pattern should work in most modern regex implementations. I can vouch for it working in Perl, Ruby, and with the PCRE regex library (which in turn means it works in PHP and BBEdit, both of which use PCRE).


This pattern attempts to be practical. It makes no attempt to parse URLs according to any official specification. It isn’t limited to predefined URL protocols. It should be clever about things like parentheses and trailing punctuation. For example, it will correctly match the URL in the following example lines:


http://foo.com/blah_blah
http://foo.com/blah_blah/
(Something like http://foo.com/blah_blah)
http://foo.com/blah_blah_(wikipedia)
(Something like http://foo.com/blah_blah_(wikipedia))
http://foo.com/blah_blah.
http://foo.com/blah_blah/.
<http://foo.com/blah_blah>
<http://foo.com/blah_blah/>
http://foo.com/blah_blah,
http://www.example.com/wpstyle/?p=364.
http://✪df.ws/e7l
rdar://1234
rdar:/1234
x-yojimbo-item://6303E4C1-xxxx-45A6-AB9D-3A908F59AE0E
message://%3c330e7f8409726r6a4ba78dkf1fd71420c1bf6ff@mail.gmail.com%3e
http://➡.ws/䨹
www.➡.ws/䨹
<tag>http://example.com</tag>
Just a www.example.com link.


It attempts to be particularly clever with regard to parentheses, which, in my experience, only ever seem to occur in the wild in Wikipedia URLs, and which many URL matching patterns seem to botch. The pattern looks for balanced parentheses within the URL, which is how it correctly omits the trailing parenthesis in the following line:


(Something like http://foo.com/blah_blah)


The pattern is also liberal about Unicode glyphs within the URL, which allows it, among other things, to match IDN domain names, such as the ✪df.ws domain I registered for the custom URL shortener I use for the Daring Fireball Twitter feed.


Suggestions and improvements are welcome, including just sending me example input where the current pattern fails.

## Facts

- Kind: [Page](https://links.thingelstad.xyz/kinds/page/)

## Same host

- [★ WWDC 2009 Wrap-Up](https://links.thingelstad.xyz/links/10229eccfd34/) (daringfireball.net, June 17, 2009)
- [★ Putting What Little We Actually Know About Chrome OS Into Context](https://links.thingelstad.xyz/links/f55af197d006/) (daringfireball.net, July 14, 2009)
- [★ Ninjawords: iPhone Dictionary, Censored by Apple](https://links.thingelstad.xyz/links/bf3bc39e11e2/) (daringfireball.net, August 5, 2009)
- [URL Shortener tr.im to Discontinue Service](https://links.thingelstad.xyz/links/0365267a4340/) (daringfireball.net, August 10, 2009)
- [Speaking of Regrets](https://links.thingelstad.xyz/links/329a81a262ae/) (daringfireball.net, October 13, 2009)

## Saved the same week

- [Mint Data Shows Online Retail Rebounding](https://links.thingelstad.xyz/links/8f6fffbb87a7/) (readwriteweb.com, November 26, 2009)
- [Lose It app for iPhone updated to 2.0, introduces online sync service](https://links.thingelstad.xyz/links/0fd5e4297f97/) (tuaw.com, November 26, 2009)
- [Apple documents iTunes LP and iTunes Extras for developers](https://links.thingelstad.xyz/links/f5c2d2fac1bf/) (tuaw.com, November 26, 2009)
- [‘It’s Like Twitter. Except We Charge People to Use It.’](https://links.thingelstad.xyz/links/d4f3c9d2803d/) (27bslash6.com, November 27, 2009)
- [Early 2010 Mac Pros to have Intel Core i9 processors with 24 logical cores?](https://links.thingelstad.xyz/links/72042d7f1f67/) (9to5mac.com, November 28, 2009)

## Elsewhere

- Wayback Machine: https://web.archive.org/web/*/http://daringfireball.net/2009/11/liberal_regex_for_matching_urls
