{
  "id": "9c3505d50557",
  "url": "http://daringfireball.net/2009/11/liberal_regex_for_matching_urls",
  "title": "★ A Liberal, Accurate Regex Pattern for Matching URLs",
  "note": "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:\n\n\n\\b(([\\w-]+://?|www[.])[^\\s()<>]+(?:\\([\\w\\d]+\\)|([^[:punct:]\\s]|/)))\n\n\nThis 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).\n\n\nThis 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:\n\n\nhttp://foo.com/blah_blah\nhttp://foo.com/blah_blah/\n(Something like http://foo.com/blah_blah)\nhttp://foo.com/blah_blah_(wikipedia)\n(Something like http://foo.com/blah_blah_(wikipedia))\nhttp://foo.com/blah_blah.\nhttp://foo.com/blah_blah/.\n<http://foo.com/blah_blah>\n<http://foo.com/blah_blah/>\nhttp://foo.com/blah_blah,\nhttp://www.example.com/wpstyle/?p=364.\nhttp://✪df.ws/e7l\nrdar://1234\nrdar:/1234\nx-yojimbo-item://6303E4C1-xxxx-45A6-AB9D-3A908F59AE0E\nmessage://%3c330e7f8409726r6a4ba78dkf1fd71420c1bf6ff@mail.gmail.com%3e\nhttp://➡.ws/䨹\nwww.➡.ws/䨹\n<tag>http://example.com</tag>\nJust a www.example.com link.\n\n\nIt 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:\n\n\n(Something like http://foo.com/blah_blah)\n\n\nThe 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.\n\n\nSuggestions and improvements are welcome, including just sending me example input where the current pattern fails.\n",
  "voice": "excerpt",
  "saved": "2009-11-28T05:11:13Z",
  "host": "daringfireball.net",
  "kind": "page",
  "tags": [],
  "record": "https://links.thingelstad.xyz/links/9c3505d50557/",
  "markdown": "https://links.thingelstad.xyz/links/9c3505d50557.md",
  "issues": [],
  "connections": {
    "same_host": {
      "count": 130,
      "page": "https://links.thingelstad.xyz/hosts/daringfireball.net/",
      "nearest": [
        {
          "id": "10229eccfd34",
          "title": "★ WWDC 2009 Wrap-Up",
          "url": "http://daringfireball.net/2009/06/wwdc09_wrapup",
          "record": "https://links.thingelstad.xyz/links/10229eccfd34/"
        },
        {
          "id": "f55af197d006",
          "title": "★ Putting What Little We Actually Know About Chrome OS Into Context",
          "url": "http://daringfireball.net/2009/07/chrome_os_context",
          "record": "https://links.thingelstad.xyz/links/f55af197d006/"
        },
        {
          "id": "bf3bc39e11e2",
          "title": "★ Ninjawords: iPhone Dictionary, Censored by Apple",
          "url": "http://daringfireball.net/2009/08/ninjawords",
          "record": "https://links.thingelstad.xyz/links/bf3bc39e11e2/"
        },
        {
          "id": "0365267a4340",
          "title": "URL Shortener tr.im to Discontinue Service",
          "url": "https://daringfireball.net/linked/2009/08/10/trim",
          "record": "https://links.thingelstad.xyz/links/0365267a4340/"
        },
        {
          "id": "329a81a262ae",
          "title": "Speaking of Regrets",
          "url": "http://daringfireball.net/linked/2009/10/13/thompson-unix",
          "record": "https://links.thingelstad.xyz/links/329a81a262ae/"
        }
      ]
    },
    "same_week": {
      "count": 13,
      "nearest": [
        {
          "id": "8f6fffbb87a7",
          "title": "Mint Data Shows Online Retail Rebounding",
          "url": "http://www.readwriteweb.com/archives/mint_data_shows_online_retail_rebounding.php",
          "record": "https://links.thingelstad.xyz/links/8f6fffbb87a7/"
        },
        {
          "id": "0fd5e4297f97",
          "title": "Lose It app for iPhone updated to 2.0, introduces online sync service",
          "url": "http://www.tuaw.com/2009/11/26/lose-it-app-for-iphone-updated-to-2-0-introduces-online-sync-s/",
          "record": "https://links.thingelstad.xyz/links/0fd5e4297f97/"
        },
        {
          "id": "f5c2d2fac1bf",
          "title": "Apple documents iTunes LP and iTunes Extras for developers",
          "url": "http://www.tuaw.com/2009/11/26/apple-documents-itunes-lp-and-itunes-extras-for-developers/",
          "record": "https://links.thingelstad.xyz/links/f5c2d2fac1bf/"
        },
        {
          "id": "d4f3c9d2803d",
          "title": "‘It’s Like Twitter. Except We Charge People to Use It.’",
          "url": "http://www.27bslash6.com/p2p.html",
          "record": "https://links.thingelstad.xyz/links/d4f3c9d2803d/"
        },
        {
          "id": "72042d7f1f67",
          "title": "Early 2010 Mac Pros to have Intel Core i9 processors with 24 logical cores?",
          "url": "http://9to5mac.com/2009/11/28/early-2010-mac-pros-to-have-intel-core-i9-processors-with-24-logical-cores/",
          "record": "https://links.thingelstad.xyz/links/72042d7f1f67/"
        }
      ]
    },
    "mentions": [],
    "mentioned_by": [],
    "saved_twice": []
  },
  "elsewhere": {
    "wayback": "https://web.archive.org/web/*/http://daringfireball.net/2009/11/liberal_regex_for_matching_urls"
  }
}
