【问题标题】:How to extract words of constant length form a paragraph?如何从段落中提取恒定长度的单词?
【发布时间】:2020-09-30 04:59:56
【问题描述】:

我正在尝试从段落/字符串中提取单词。我搜索了很多地方,但没有找到相关材料。我想从

中提取长度为 4 的单词

“我想在年纪大一点的时候有很多钱,可能是 e1X2”

我正在尝试使用

进行提取
List<String> words = new ArrayList<String>();
        String s  = "I want to have alot of moneys when I am older probably.";
        Pattern p = Pattern.compile("[a-zA-Z']{4,}");
        Matcher m = p.matcher(s);
        while (m.find()) {
            words.add(m.group());
        }

    System.out.println(words);

我现在得到的输出

[want, have, alot, moneys, when, older, probably]

但输出必须是

[want, have, alot, when]

【问题讨论】:

  • 单词可以包含数字吗?
  • 只需将 {4,} 更改为 {4}
  • 是的,它也可以包含数字
  • @RadheAnkit - 查看我更新的问题
  • 我认为您可能需要添加look-ahead 和look-behind 以确保有一个空格字符(或字符串开头或结尾)分隔单词

标签: java android substring text-extraction stringtokenizer


【解决方案1】:

你想用正则表达式来做吗?

因为您没有放置表示组的“()”,因为您调用了“m.group()”,所以您需要这种语法。

在这里使用您的正则表达式:regex101。然后将其放入您的 Java 程序中。

您也可以用空格分割字符串,并仅使用具有所需长度的元素过滤结果数组。

【讨论】:

    【解决方案2】:

    获取结果的更简单方法:

    List<String> words=new ArrayList<String>(); 
        String s="I want to have alot of of moneys when I am older probably";
        String str[]=s.split(" ");
        for(int i=0;i<str.length;i++)
        {
            if(str[i].length()==4)
                words.add(str[i]);
        }
        System.out.print(words);
    

    【讨论】:

      【解决方案3】:

      试试:

      public static void main(String[] args) {
      
              List<String> words = new ArrayList<String>();
              String s  = "I want to have alot of moneys when I am older probably.";
              Pattern p = Pattern.compile("\\b\\w{4}\\b");
              Matcher m = p.matcher(s);
              while (m.find()) {
                  words.add(m.group());
              }
      
              System.out.println(words);
          }
      

      输出: [want, have, alot, when]

      说明:

      1. \b 匹配单词边界。

      【讨论】:

      • 感谢兄弟您的回答也有效,但我必须接受先到先得的原则。但你值得 +1
      • @IrfanAkram 呵呵没问题兄弟。我有点晚了,因为我只想用正则表达式解决你的问题,因为你在你的问题中使用了它。无论如何,感谢您的支持。下次好运!!!
      【解决方案4】:

      您需要在您的正则表达式中向后看并向前看

      您的原件:

          Pattern p = Pattern.compile("[a-zA-Z']{4,}");
      

      向前看,向后看:

          Pattern p = Pattern.compile("(?=\s)[a-zA-Z']{4,}(?=\s)");
      

      现在已经添加了前瞻和后随,可能存在字符串开头和结尾不匹配的问题。在匹配字符串的两侧添加一个空格,它应该可以工作

      【讨论】:

        【解决方案5】:

        使用stream API的解决方案

        /* Required imports:
         * import java.util.Arrays;
         * import java.util.List;
         * import java.util.stream.Collectors;
         */
        List<String> words = Arrays.stream(text.split("\\b"))
                                   .filter(word -> word.length() == 4)
                                   .collect(Collectors.toList());
        

        文本被拆分成单独的单词。
        只有长度为 4 的词才能通过过滤器。
        所有四个字母的单词都被收集到一个列表中。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-07-29
          • 2020-06-17
          • 2021-05-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多