开发者

How to parse out html links from a huge string with html links and other text (Java)

开发者 https://www.devze.com 2022-12-27 13:58 出处:网络
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 i

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 arr开发者_如何学JAVAay, 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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消