{"id":1248,"date":"2014-03-29T11:38:27","date_gmt":"2014-03-29T15:38:27","guid":{"rendered":"http:\/\/andrewpallant.ca\/wordpress\/?p=1248"},"modified":"2014-03-29T12:13:24","modified_gmt":"2014-03-29T16:13:24","slug":"auto-link-using-regular-expressions","status":"publish","type":"post","link":"http:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/","title":{"rendered":"Auto-Link Using Regular Expressions"},"content":{"rendered":"<p><a href=\"http:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2014\/03\/link.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft  wp-image-1253\" alt=\"Auto-Linking\" src=\"http:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2014\/03\/link.jpg\" width=\"180\" height=\"180\" srcset=\"http:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2014\/03\/link.jpg 225w, http:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2014\/03\/link-150x150.jpg 150w\" sizes=\"(max-width: 180px) 100vw, 180px\" \/><\/a>I was recently asked if I could automatically turn website text (ex: www.google.com ) into HTML hyperlinks. \u00a0My first thought was ah CRAP! \u00a0I also wondered why they could not use the link tool in the editor, but they asked so I delivered. With about 5 minutes on Google, I found the perfect solution that worked for me.<\/p>\n<p>Mind you I was working in C# for this, but since it is a regular expression solution; it will apply to virtually any language. \u00a0The one change that I had made from the original code was to add in a piece that also auto-linked when the text included &#8220;http:\/\/www.&#8221;. \u00a0You may or may not want the extra addition that I had made.<\/p>\n<p><!--more--><\/p>\n<p>Original Article:\u00a0<a href=\"http:\/\/stackoverflow.com\/questions\/3037623\/automatically-hyper-link-urls-and-emails-using-c-whilst-leaving-bespoke-tags\" target=\"_blank\">http:\/\/stackoverflow.com\/<wbr \/>questions\/3037623\/<wbr \/>automatically-hyper-link-urls-<wbr \/>and-emails-using-c-whilst-<wbr \/>leaving-bespoke-tags<\/a><\/p>\n<pre style=\"font-family: arial; font-size: 12px; border: 1px dashed #CCCCCC; width: 99%; height: auto; overflow: auto; background: #f0f0f0; ;background-image: url('http:\/\/2.bp.blogspot.com\/_z5ltvMQPaa8\/SjJXr_U2YBI\/AAAAAAAAAAM\/46OqEP32CJ8\/s320\/codebg.gif'); padding: 0px; color: #000000; text-align: left; line-height: 20px;\"><code style=\"color: #000000; word-wrap: normal;\"> public static string ActivateLinksInText(string source)  \r\n   {  \r\n     source = \" \" + source + \" \";  \r\n     \/\/ easier to convert BR's to something more neutral for now.  \r\n     source = Regex.Replace(source, \"&lt;br&gt;|&lt;br \/&gt;|&lt;br\/&gt;\", \"\\n\");  \r\n     source = Regex.Replace(source, @\"([\\s])(www\\..*?|http:\/\/.*?)([\\s])\", \"$1&lt;a href=\\\"$2\\\" target=\\\"_blank\\\"&gt;$2&lt;\/a&gt;$3\");  \r\n     source = Regex.Replace(source, @\"([\\s])(http:\/\/www\\..*?)([\\s])\", \"$1&lt;a href=\\\"$2\\\" target=\\\"_blank\\\"&gt;$2&lt;\/a&gt;$3\");  \r\n     source = Regex.Replace(source, @\"href=\"\"www\\.\", \"href=\\\"http:\/\/www.\");  \r\n     \/\/source = Regex.Replace(source, \"\\n\", \"&lt;br \/&gt;\");  \r\n     return source.Trim();  \r\n   }  \r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I was recently asked if I could automatically turn website text (ex: www.google.com ) into HTML hyperlinks. \u00a0My first thought was ah CRAP! \u00a0I also wondered why they could not use the link tool in the editor, but they asked so I delivered. With about 5 minutes on Google, I found the perfect solution that &hellip; <a href=\"http:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Auto-Link Using Regular Expressions<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,14,141,36,96],"tags":[180,236,199,198,252],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\r\n<title>Auto-Link Using Regular Expressions - Software Developer In London by Andrew Pallant<\/title>\r\n<meta name=\"description\" content=\"Auto-Link Using Regular Expressions - Software Developer In London by Andrew Pallant\" \/>\r\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/\" \/>\r\n<meta property=\"og:locale\" content=\"en_US\" \/>\r\n<meta property=\"og:type\" content=\"article\" \/>\r\n<meta property=\"og:title\" content=\"Auto-Link Using Regular Expressions - Software Developer In London by Andrew Pallant\" \/>\r\n<meta property=\"og:description\" content=\"Auto-Link Using Regular Expressions - Software Developer In London by Andrew Pallant\" \/>\r\n<meta property=\"og:url\" content=\"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/\" \/>\r\n<meta property=\"og:site_name\" content=\"LDNDeveloper\" \/>\r\n<meta property=\"article:published_time\" content=\"2014-03-29T15:38:27+00:00\" \/>\r\n<meta property=\"article:modified_time\" content=\"2014-03-29T16:13:24+00:00\" \/>\r\n<meta property=\"og:image\" content=\"http:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2014\/03\/link.jpg\" \/>\r\n<meta name=\"author\" content=\"ldnDeveloper\" \/>\r\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\r\n<meta name=\"twitter:creator\" content=\"@LdnDeveloper\" \/>\r\n<meta name=\"twitter:site\" content=\"@LdnDeveloper\" \/>\r\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"ldnDeveloper\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\r\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/\"},\"author\":{\"name\":\"ldnDeveloper\",\"@id\":\"http:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84\"},\"headline\":\"Auto-Link Using Regular Expressions\",\"datePublished\":\"2014-03-29T15:38:27+00:00\",\"dateModified\":\"2014-03-29T16:13:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/\"},\"wordCount\":132,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84\"},\"image\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2014\/03\/link.jpg\",\"keywords\":[\"c#\",\"DotNet\",\"RegEx\",\"Regular Expressions\",\"Web\"],\"articleSection\":[\"C#\",\"C-Sharp\",\"Developement\",\"How To\",\"Web\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/\",\"url\":\"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/\",\"name\":\"Auto-Link Using Regular Expressions - Software Developer In London by Andrew Pallant\",\"isPartOf\":{\"@id\":\"http:\/\/andrewpallant.ca\/wordpress\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2014\/03\/link.jpg\",\"datePublished\":\"2014-03-29T15:38:27+00:00\",\"dateModified\":\"2014-03-29T16:13:24+00:00\",\"description\":\"Auto-Link Using Regular Expressions - Software Developer In London by Andrew Pallant\",\"breadcrumb\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/#primaryimage\",\"url\":\"http:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2014\/03\/link.jpg\",\"contentUrl\":\"http:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2014\/03\/link.jpg\",\"width\":225,\"height\":225,\"caption\":\"Auto-Linking\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/andrewpallant.ca\/wordpress\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Auto-Link Using Regular Expressions\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/andrewpallant.ca\/wordpress\/#website\",\"url\":\"http:\/\/andrewpallant.ca\/wordpress\/\",\"name\":\"LDNDeveloper\",\"description\":\"Learning, Growing and Sharing.\",\"publisher\":{\"@id\":\"http:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/andrewpallant.ca\/wordpress\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84\",\"name\":\"ldnDeveloper\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2017\/05\/cropped-AAEAAQAAAAAAAAXQAAAAJDQxMGRlMzFjLWM4ODctNDk1NC05M2EyLWE1NDNhNTRiZjVlYw-2.jpg\",\"contentUrl\":\"https:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2017\/05\/cropped-AAEAAQAAAAAAAAXQAAAAJDQxMGRlMzFjLWM4ODctNDk1NC05M2EyLWE1NDNhNTRiZjVlYw-2.jpg\",\"width\":512,\"height\":512,\"caption\":\"ldnDeveloper\"},\"logo\":{\"@id\":\"http:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/image\/\"},\"description\":\"Andrew Pallant (@LdnDeveloper) has been a web, database and desktop developer for over 16 years. Andrew has worked on projects that ranged from factory automation to writing business applications. Most recently he has been heavily involved in various forms for ecommerce projects. Over the years Andrew has worn many hats: Project Manager, IT Manager, Lead Developer, Supervisor of Developers and many more - See more at: http:\/\/www.unlatched.com\/#sthash.8DiTkpKy.dpuf\",\"sameAs\":[\"http:\/\/www.andrewpallant.ca\",\"https:\/\/x.com\/LdnDeveloper\"],\"url\":\"http:\/\/andrewpallant.ca\/wordpress\/author\/ldndeveloper\/\"}]}<\/script>\r\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Auto-Link Using Regular Expressions - Software Developer In London by Andrew Pallant","description":"Auto-Link Using Regular Expressions - Software Developer In London by Andrew Pallant","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/","og_locale":"en_US","og_type":"article","og_title":"Auto-Link Using Regular Expressions - Software Developer In London by Andrew Pallant","og_description":"Auto-Link Using Regular Expressions - Software Developer In London by Andrew Pallant","og_url":"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/","og_site_name":"LDNDeveloper","article_published_time":"2014-03-29T15:38:27+00:00","article_modified_time":"2014-03-29T16:13:24+00:00","og_image":[{"url":"http:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2014\/03\/link.jpg"}],"author":"ldnDeveloper","twitter_card":"summary_large_image","twitter_creator":"@LdnDeveloper","twitter_site":"@LdnDeveloper","twitter_misc":{"Written by":"ldnDeveloper","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/#article","isPartOf":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/"},"author":{"name":"ldnDeveloper","@id":"http:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84"},"headline":"Auto-Link Using Regular Expressions","datePublished":"2014-03-29T15:38:27+00:00","dateModified":"2014-03-29T16:13:24+00:00","mainEntityOfPage":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/"},"wordCount":132,"commentCount":0,"publisher":{"@id":"http:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84"},"image":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/#primaryimage"},"thumbnailUrl":"http:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2014\/03\/link.jpg","keywords":["c#","DotNet","RegEx","Regular Expressions","Web"],"articleSection":["C#","C-Sharp","Developement","How To","Web"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/","url":"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/","name":"Auto-Link Using Regular Expressions - Software Developer In London by Andrew Pallant","isPartOf":{"@id":"http:\/\/andrewpallant.ca\/wordpress\/#website"},"primaryImageOfPage":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/#primaryimage"},"image":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/#primaryimage"},"thumbnailUrl":"http:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2014\/03\/link.jpg","datePublished":"2014-03-29T15:38:27+00:00","dateModified":"2014-03-29T16:13:24+00:00","description":"Auto-Link Using Regular Expressions - Software Developer In London by Andrew Pallant","breadcrumb":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/#primaryimage","url":"http:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2014\/03\/link.jpg","contentUrl":"http:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2014\/03\/link.jpg","width":225,"height":225,"caption":"Auto-Linking"},{"@type":"BreadcrumbList","@id":"https:\/\/andrewpallant.ca\/wordpress\/auto-link-using-regular-expressions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/andrewpallant.ca\/wordpress\/"},{"@type":"ListItem","position":2,"name":"Auto-Link Using Regular Expressions"}]},{"@type":"WebSite","@id":"http:\/\/andrewpallant.ca\/wordpress\/#website","url":"http:\/\/andrewpallant.ca\/wordpress\/","name":"LDNDeveloper","description":"Learning, Growing and Sharing.","publisher":{"@id":"http:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/andrewpallant.ca\/wordpress\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"http:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84","name":"ldnDeveloper","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/image\/","url":"https:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2017\/05\/cropped-AAEAAQAAAAAAAAXQAAAAJDQxMGRlMzFjLWM4ODctNDk1NC05M2EyLWE1NDNhNTRiZjVlYw-2.jpg","contentUrl":"https:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2017\/05\/cropped-AAEAAQAAAAAAAAXQAAAAJDQxMGRlMzFjLWM4ODctNDk1NC05M2EyLWE1NDNhNTRiZjVlYw-2.jpg","width":512,"height":512,"caption":"ldnDeveloper"},"logo":{"@id":"http:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/image\/"},"description":"Andrew Pallant (@LdnDeveloper) has been a web, database and desktop developer for over 16 years. Andrew has worked on projects that ranged from factory automation to writing business applications. Most recently he has been heavily involved in various forms for ecommerce projects. Over the years Andrew has worn many hats: Project Manager, IT Manager, Lead Developer, Supervisor of Developers and many more - See more at: http:\/\/www.unlatched.com\/#sthash.8DiTkpKy.dpuf","sameAs":["http:\/\/www.andrewpallant.ca","https:\/\/x.com\/LdnDeveloper"],"url":"http:\/\/andrewpallant.ca\/wordpress\/author\/ldndeveloper\/"}]}},"_links":{"self":[{"href":"http:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/posts\/1248"}],"collection":[{"href":"http:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/comments?post=1248"}],"version-history":[{"count":6,"href":"http:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/posts\/1248\/revisions"}],"predecessor-version":[{"id":1255,"href":"http:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/posts\/1248\/revisions\/1255"}],"wp:attachment":[{"href":"http:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/media?parent=1248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/categories?post=1248"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/tags?post=1248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}