如何从文本中提取单个单词和网址?

时间:2019-02-08 11:40:59

标签: java url text

我正在尝试从文本中提取单词。我的文字看起来像这样:

String text = "This is my text and it contains a url: http://www.google.com";
String[] words = text.split("\\W+");

这里的问题是,这种方式将url分解为诸如http www google com之类的单词,对我来说毫无用处。

我想拥有的是这样的数组:

This
is
my
text
and
it
contains
a
url
http://www.google.com

有什么想法/建议吗?

2 个答案:

答案 0 :(得分:0)

String[] words = text.split(" ");

将返回[这是我的文本,并且包含a,url :, http://www.google.com]

答案 1 :(得分:0)

我愿意

text.split("\\s+");

在正则表达式中,\s匹配任何空格字符,而split(" ")只能匹配Unicode \u0020