【问题标题】:How can I extract individual words and urls from a text?如何从文本中提取单个单词和 url?
【发布时间】:2019-02-08 11:40:58
【问题描述】:

我正在尝试从文本中提取单词。我的文字如下所示:

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

有什么想法/建议吗?

【问题讨论】:

    标签: java url text


    【解决方案1】:
    String[] words = text.split(" ");
    

    将返回 [This, is, my, text, and, it, contains, a, url:, http://www.google.com]

    【讨论】:

      【解决方案2】:

      我会去

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

      在正则表达式中,\s 匹配 任何 个空白字符,而 split(" ") 仅匹配 unicode \u0020

      【讨论】:

        猜你喜欢
        • 2021-03-22
        • 2023-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-14
        • 1970-01-01
        相关资源
        最近更新 更多