views:

40

answers:

1

Hey,

I'm trying to replace all non-anchor-tag-enclosed URLs within anchor-tag-enclosed URLs for a document. So given the string:

I have two urls for google: <a href="http://www.google.com/"&gt;google&lt;/a&gt; and http://www.google.com/

I would like to replace it with this:

I have two urls for google: <a href="http://www.google.com/"&gt;google&lt;/a&gt; and <a href="http://www.google.com/"&gt;http://www.google.com/&lt;/a&gt;

Does anyone know a clean way to do this in Java?

A: 

How about this? Not exactly clean, but it works.

    String str = "I have two urls for google: <a href=\"http://www.google.com/\"&gt;google&lt;/a&gt; and http://www.one.com/ something http://www.two.com/asd  else";

    Pattern p = Pattern.compile("\\s(http\\:\\/\\/[\\w\\W]*?)\\s");     
    Matcher m = p.matcher(str);
    StringBuffer sb = new StringBuffer(1000);
    while (m.find()) {
        String link = m.group(1);
        m.appendReplacement(sb, " <a href=\"" + link + "\">" + link + "</a> ");
    }

    m.appendTail(sb);

    System.out.println(sb.toString());
limc
Close... but this doesn't work if the URL is enclosed in some other type of HTML tag, for exampleString str = "<p>http://www.google.com/</p>";should print<p><a href="http://www.google.com/">http://www.google.com</a></p>
Ben