views:

48

answers:

2

Hello, my question is how would i be able to go through a string and take out only the links and erase all the rest? I thought about using some type of delimiter, but wouldn't know how to go about using it in Java. an example of what i am trying to do:

this is my String:

String myString = "The file is http: // www.   .com/hello.txt and the second file is "
                     + "http: // www.   .com/hello2.dat";

I would want the output to be:

"http: // www.   .com/hello.txt http: // www.   .com/hello2.dat"

or each could be added to an array, separately. I just want some ideas, id like to write the code myself but am having trouble on how to do it. Any help would be awesome.

+2  A: 

You definitely want to use a regular expression. You'll need to find a good one for matching URLs, and see Java's Pattern and Matcher classes

Michael Mrozek
Thank you, I read about those things a few hours ago but wasnt sure if it was important. I'm going to look for some examples of them in action.
Robert
+1  A: 

Regular Expressions, or Regex, is built for this kind of work. It is like another mini-language to learn. The best book out there some would say is Mastering Regular Expressions

The Javadoc for Pattern and Matcher can only serve as a reference. It completely ignores the subtleties involved in regex.

Vincent
Thanks, I realized that its going to take some time until i can to what i set out to do. I will read the book and see if it helps.
Robert