Java function needed to take a specific text and change it to url
<p>I need a Java code to take this text " text1 [example1]http://example1.com [example2]http://example2.com text2 ...." and return it as the following:</p>

<pre><code>text1 &lt;a target='_blank' href='http://example1.com'&gt;example1&lt;/a&gt; &lt;a target='_blank' href='http://example2.com'&gt;example2&lt;/a&gt; text2
</code></pre>

<p>i mean whenever it finds this pattern [example2]http://example2.com in a text to take and put it in html hyperlink. Please help.. that what i have done till now but its not working </p>

<pre><code>    ViolationTableItem item = new ViolationTableItem(m_violation);
    String dataSource = "";
     String copyDataSource="";
    try {
        dataSource = item.getUi().getViolation().getBlackListEntries()
                .getDataSources();

        Matcher matcher = Pattern.compile(
                "(\\[.*?\\])(.*://[^&lt;&gt;[:space:]]+[[:alnum:]/])").matcher(
                dataSource);

        while (matcher.find()) {

            String matchedLink = matcher.group();

            Matcher nameMatcher = Pattern.compile("\\[.*?\\]").matcher(
                    matchedLink);
            String nameMatched = "";
            String nameMatched2 = "";
            String linkableText = "";
            String[] tst = matchedLink.split("\\[.*?\\]");
            for (int i = 1; i &lt; tst.length; i++) {
                if (nameMatcher.find()) {
                    nameMatched = nameMatcher.group();
                    // System.out.println(nameMatched);
                    nameMatched2 = matchedLink.replace(nameMatched, "");
                    // System.out.println(nameMatched2);

                }
                if (tst[i] != null &amp;&amp; !tst[i].equals("")) {
                    linkableText = "&lt;a target='_blank' href='"
                            + tst[i]
                            + "'&gt;"
                            + nameMatched.replaceAll("\\[", "").replaceAll(
                                    "\\]", "") + "&lt;/a&gt;";
                    copyDataSource += dataSource.replace(matchedLink,
                            linkableText) + " ";
                }
            }

        }
    } catch (Exception ne){
        return copyDataSource;
    }
    return copyDataSource;
</code></pre>

<p>Thanks in advance!</p>
