Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: using regex to find URL turning them into html link. Also detect if link contain http://, If not, append it
    primarykey
    data
    text
    <p>I know this questions get ask a lot, and <code>Kelly Chan</code> did provide an answer that is work for me, however, there still minor problem that I hope the community can help me out. </p> <p>For example if a user type this:</p> <pre><code>Please visit www.google.com </code></pre> <p>Then I want to convert it into this</p> <pre><code>Please visit &lt;a href="http://www.google.com"&gt;www.google.com&lt;/a&gt; </code></pre> <p><strong>NOTE</strong>: that the original text only contain <code>www.google.com</code>, but I somehow detect that it need to have <code>http://</code> in front of it. so the link become <code>&lt;a href="http://www.google.com"&gt;www.google.com&lt;/a&gt;</code>. If the link is <code>http://www.google.com</code>, then I just need to wrap it around <code>&lt;a href&gt;</code>. </p> <p><strong>EDIT</strong>: <code>Kelly Chan</code> has revised her answer and it work. Below is the solution.</p> <pre><code> Pattern patt = Pattern.compile("(?i)\\b((?:https?://|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()&lt;&gt;]+|\\(([^\\s()&lt;&gt;]+|(\\([^\\s()&lt;&gt;]+\\)))*\\))+(?:\\(([^\\s()&lt;&gt;]+|(\\([^\\s()&lt;&gt;]+\\)))*\\)|[^\\s`!()\\[\\]{};:\'\".,&lt;&gt;???“”‘’]))"); Matcher matcher = patt.matcher(this.mytext); if(matcher.find()){ if (matcher.group(1).startsWith("http://")){ return matcher.replaceAll("&lt;a href=\"$1\"&gt;$1&lt;/a&gt;"); }else{ return matcher.replaceAll("&lt;a href=\"http://$1\"&gt;$1&lt;/a&gt;"); } }else{ return this.mytext } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload