regular expression search replace

Discussion in 'Java' started by J. VerSchave, Feb 28, 2004.

  1. J. VerSchave

    J. VerSchave Guest

    I am trying to do this using regular expressions in Java:

    replace

    http://whatever.com

    with

    <a href="http://whatever.com">http://whatever.com</a>



    This URL is embedded within a String, i.e.:

    String buffer = "The other I came across http://whatever.com. It is
    cool.";

    I want to perform some operation on this buffer and have the result
    be:

    "The other I came across <a
    href="http://whatever.com">http://whatever.com</a>. It is cool."


    Seems like this should be easy to do but I have been unsuccessful thus
    far in finding a solution. Thanks.

    -j
     
    J. VerSchave, Feb 28, 2004
    #1
    1. Advertising

  2. J. VerSchave

    Bobo Guest

    buffer = buffer.replaceFirst("http://whatever.com",
    "<a
    href=\"http://whatever.com\">http://whatever.com</a>");

    You have to be running JDK 1.4 or up, to use built-in regular expression
    support.

    Paul


    "J. VerSchave" <> wrote in message
    news:...
    > I am trying to do this using regular expressions in Java:
    >
    > replace
    >
    > http://whatever.com
    >
    > with
    >
    > <a href="http://whatever.com">http://whatever.com</a>
    >
    >
    >
    > This URL is embedded within a String, i.e.:
    >
    > String buffer = "The other I came across http://whatever.com. It is
    > cool.";
    >
    > I want to perform some operation on this buffer and have the result
    > be:
    >
    > "The other I came across <a
    > href="http://whatever.com">http://whatever.com</a>. It is cool."
    >
    >
    > Seems like this should be easy to do but I have been unsuccessful thus
    > far in finding a solution. Thanks.
    >
    > -j
     
    Bobo, Feb 29, 2004
    #2
    1. Advertising

  3. J. VerSchave

    Alan Moore Guest

    On 28 Feb 2004 15:22:09 -0800, (J. VerSchave)
    wrote:

    >I am trying to do this using regular expressions in Java:
    >
    >replace
    >
    >http://whatever.com
    >
    >with
    >
    ><a href="http://whatever.com">http://whatever.com</a>
    >
    >
    >
    >This URL is embedded within a String, i.e.:
    >
    >String buffer = "The other I came across http://whatever.com. It is
    >cool.";
    >
    >I want to perform some operation on this buffer and have the result
    >be:
    >
    >"The other I came across <a
    >href="http://whatever.com">http://whatever.com</a>. It is cool."
    >
    >
    >Seems like this should be easy to do but I have been unsuccessful thus
    >far in finding a solution. Thanks.
    >
    >-j


    How complex does it need to be? For the example you gave, this should
    work:

    String result = buffer.replaceAll("http://\\S+(?<=\\w)",
    "<a href=\"$0\">$0</a>");

    The "\\S+" matches everything up to the next whitespace character,
    then the lookbehind, "(?<=\\w)", cause the match to back up, if
    necessary, until the last character matched is a letter, digit, or
    underscore. That's just a quick and dirty way to keep the period at
    the end of the sentence or other punctuation) from being included in
    the URL.

    But what about https and ftp links? Or mailto links? It's easy
    enough to include those:

    String result = buffer.replaceAll(
    "((https?|ftp)://|mailto:)\\S+(?<=\\w)",
    "<a href=\"$0\">$0</a>");


    The real fun starts when you need to capture URLs that don't have the
    protocol prefix, e.g., "whatever.com". But I won't go into that
    unless you need it.

    Alan
     
    Alan Moore, Feb 29, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    450
  2. VSK
    Replies:
    2
    Views:
    2,390
  3. lucky
    Replies:
    0
    Views:
    350
    lucky
    Feb 9, 2005
  4. Jimmy
    Replies:
    25
    Views:
    805
    Jeff Higgins
    May 26, 2010
  5. Jimmy
    Replies:
    13
    Views:
    456
    Arne Vajhøj
    Jul 25, 2012
Loading...

Share This Page