{"id":927,"date":"2018-09-17T18:30:41","date_gmt":"2018-09-17T09:30:41","guid":{"rendered":"https:\/\/chat-messenger.com\/?p=927"},"modified":"2018-12-23T02:00:46","modified_gmt":"2018-12-22T17:00:46","slug":"geturl-link-from-string","status":"publish","type":"post","link":"https:\/\/chat-messenger.com\/en\/blog\/java\/geturl-link-from-string","title":{"rendered":"Convert URLs in Java strings to links"},"content":{"rendered":"<h2 class=\"common_title\"><a name=\"0\">Java <strong>(computer) regular expression<\/strong>in a string using<strong>uniform resouce locator<\/strong>indicates object of desire, like, hate, etc.<strong>link<\/strong>\uff08Convert to (a href=...)<\/a><\/h2>\n<p>\nin a string in Java.<strong>uniform resouce locator<\/strong>using a regular expression,<strong>link<\/strong>(a href=...)<strong>conversion<\/strong>The following is a sample source that will be used.\n<\/p>\n<h2 class=\"common_title\"><a name=\"1\">source code<\/a><\/h2>\n<pre style=\"overflow-y:auto;\" class=\"sample_src\">\/** regular expression pattern for extracting URL *\/\r\npublic static final Pattern convURLLinkPtn =\r\n    Pattern.compile\r\n    (&quot;(http:\/\/|https:\/\/){1}[\\\\w\\. \\\\-\/:\\\\#\\? \\\\=\\\\&amp;amp;\\\\\\;\\\\%\\\\~\\\\+]+&quot;,\r\n    Pattern.CASE_INSENSITIVE);\r\n\r\n\/**\r\n * URLs in the specified string, using regular expressions,\r\n * link (a href=...) to a link (a href=...) using a regular expression.\r\n * @param str Specified string.\r\n * @return String converted to a link.\r\n *\/\r\npublic static String convURLLink(String str) {\r\n    Matcher matcher = convURLLinkPtn.matcher(str);\r\n    return matcher.replaceAll(&quot;&lt;a href=&quot;\/en\/\\&quot;$0\\&quot;\/&quot;&gt;$0&lt;\/a&gt;&quot;);\r\n}\r\n<\/pre>\n<p><script type=\"text\/javascript\" src=\"https:\/\/chat-messenger.com\/js\/common.js?dd12sssas2223\" charset=\"UTF-8\"><\/script><br \/>\n<script type=\"text\/javascript\"> writePR(); <\/script><\/p>\n<h2 class=\"common_title\"><a name=\"2\">Execution Result<\/a><\/h2>\n<p>\u25c6Example of Execution<\/p>\n<pre style=\"height:250px;overflow-y:auto;\" class=\"sample_src\">public static void main(String[] args) {\r\n    \/\/ simple pattern\r\n    String ret1 = convURLLink(\"https:\/\/chat-messenger.com\/\");\r\n    System.out.println(ret1);\r\n\r\n    \/\/ This can be used for patterns with complex URLs embedded in the string.\r\n    String ret2 = convURLLink(\"Hello.\" +\r\n            \"http:\/\/aaa.co.jp\/sup\/topic.py?dep_id=108&amp;~id=108\" +\r\n            \"Visit the URL here.\") ;\r\n    System.out.println(ret2);\r\n}\r\n<\/pre>\n<p>\u25c6Output result<\/p>\n<div style=\"height:100px;overflow-y:auto;\" class=\"console\">\n&lt;a href=&quot;\/en\/&rdquo;https:\/\/chat-messenger.com\/&rdquo;\/&quot;&gt;https:\/\/chat-messenger.com\/&lt;\/a&gt;<\/p>\n<p>Hello.&lt;a http:&gt;http:\/\/aaa.co.jp\/sup\/topic.py?dep_id=108&amp;~id=108&lt;\/a&gt;Go to the URL here.\n<\/p><\/div>","protected":false},"excerpt":{"rendered":"<p>Convert the URL in a string to a link (a href=\u2026) using Java regular expressions Convert [\u2026] in a string in Java<\/p>","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"swell_btn_cv_data":""},"categories":[19],"tags":[],"_links":{"self":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/927"}],"collection":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/comments?post=927"}],"version-history":[{"count":4,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/927\/revisions"}],"predecessor-version":[{"id":1890,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/927\/revisions\/1890"}],"wp:attachment":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/media?parent=927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/categories?post=927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/tags?post=927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}