find URL and E-Mail addresses

Hi,

how can I find URLs and E-Mail adresses in a Textbox?

My idea: If found, make a link on it.

I've tried with

if (txt.matches("(w{3})\\.(.*)\\.com")) {

// create a link

}

to search for an URL like "www.domain.com", but it doesn't work! Why?

And how can I get the URL he finally found? So I can replace it with a link...

Thank you for your answers.

[413 byte] By [Killi138a] at [2007-10-2 1:37:07]
# 1

well maybe this is not the correct place to ask this because the question does not directly relate to design patterns or oo design. However you could take a look into regular expressions. e.g. right there :

http://java.sun.com/docs/books/tutorial/extra/regex/

hope that helps

jan

jan_techa at 2007-7-15 19:00:28 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

You can't, not reliably at least.

Your checking (or attempting to) for "www" at the start of a String is too restrictive. URLs can be pretty much anything after all. A better way might be to check for :// which will usually indicate you're dealing with a url of some sort (but of course many people don't use a protocol when referring to websites).

Email might pretty usually contain a single @-sign at some place that's not the very beginning or end of the String, but not always.

Many internal mailsystems work perfectly fine without entering the domain name for example, assuming it's internal when no domain is given, and there are mail systems that don't use a user/domain designation at all or use a different separator.

A more useful means is to designate a set input field as a URL field or an email field and assume that what's there is a URL or email address.

jwentinga at 2007-7-15 19:00:28 > top of Java-index,Other Topics,Patterns & OO Design...