Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > regular expression search replace

Reply
Thread Tools

regular expression search replace

 
 
J. VerSchave
Guest
Posts: n/a
 
      02-28-2004
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
 
Reply With Quote
 
 
 
 
Bobo
Guest
Posts: n/a
 
      02-29-2004
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: om...
> 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



 
Reply With Quote
 
 
 
 
Alan Moore
Guest
Posts: n/a
 
      02-29-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
need regular expression to replace part of result based on a search pattern Jimmy Java 13 07-25-2012 09:37 PM
Regular expression search and replace Jimmy Java 25 05-26-2010 06:33 PM
Search, Replace with Regular Expression tushar.n.patel@gmail.com ASP .Net 1 02-09-2005 10:09 PM
Search, Replace with Regular Expression lucky ASP .Net 0 02-09-2005 07:10 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments