【问题标题】:How can I find compound string in text如何在文本中找到复合字符串
【发布时间】:2018-07-24 09:26:45
【问题描述】:

我一直在寻找解决方案来在句子中找到像 howareyou 这样的字符串并将它们从中删除。例如:

我们有一句话——Hello there, how are you?

和复合 - how are you 结果,我想要这个字符串 - Hello there, ? 去除化合物。

我当前的解决方案是将字符串拆分为单词并检查复合词是否包含每个单词,但效果不佳,因为如果您还有其他与该复合词匹配的单词,它们也会被删除,例如:

如果我们将在此字符串中查找foreseenfuture - I have foreseen future for all of you,那么根据我的解决方案for 也将被删除,因为它在化合物内部。

代码

String[] words = text.split("[^a-zA-Z]");
String compound = "foreseenfuture";

int startIndex = -1;
int endIndex = -1;

for(String word : words){
  if(compound.contains(word)){
     if(startIndex == -1){
       startIndex = text.indexOf(word);
     }

     endIndex =  text.indexOf(word) + word.length() - 1;
  }
}

if(startIndex != -1 && endIndex != -1){
  text = text.substring(0, startIndex) + "" + text.substring(endIndex + 1, text.length() - 1);
}

那么,有没有其他方法可以解决这个问题?

【问题讨论】:

  • 试试this
  • 复合应该没有空格。
  • “应该”或“是”没有空格?
  • 是,对不起。就像在我的代码中一样 - String compound = "foreseenfuture";
  • 从字符串中删除foreseenfuture 后的最终结果是什么?

标签: java regex string search text


【解决方案1】:

我假设当你复合时你只删除空格。因此,在这种假设下,“for,seen future.for seen future”将变为“for,seen future.”,因为逗号将另一个复合词分开。在这种情况下,这应该可以工作:

    String example1 = "how are you?";
    String example2 = "how, are you... here?";
    String example3 = "Madam, how are you finding the accommodations?";
    String example4 = "how are you how are you how are you taco";

    String compound = "howareyou";

    StringBuilder compoundRegexBuilder = new StringBuilder();

    //This matches to a word boundary before the first word
    compoundRegexBuilder.append("\\b");

    // inserts each character into the regex
    for(int i = 0; i < compound.length(); i++) {
        compoundRegexBuilder.append(compound.charAt(i));

        // between each letter there could be any amount of whitespace
        if(i<compound.length()-1) {
            compoundRegexBuilder.append("\\s*");
        }
    }

    // Makes sure the last word isn't part of a larger word
    compoundRegexBuilder.append("\\b");

    String compoundRegex = compoundRegexBuilder.toString();
    System.out.println(compoundRegex);
    System.out.println("Example 1:\n" + example1 + "\n" + example1.replaceAll(compoundRegex, ""));
    System.out.println("\nExample 2:\n" + example2 + "\n" + example2.replaceAll(compoundRegex, ""));
    System.out.println("\nExample 3:\n" + example3 + "\n" + example3.replaceAll(compoundRegex, ""));
    System.out.println("\nExample 4:\n" + example4 + "\n"  + example4.replaceAll(compoundRegex, ""));

输出如下:

\bh\s*o\s*w\s*a\s*r\s*e\s*y\s*o\s*u\b
Example 1:
how are you?
?

Example 2:
how, are you... here?
how, are you... here?

Example 3:
Madam, how are you finding the accommodations?
Madam,  finding the accommodations?

Example 4:
how are you how are you how are you taco
   taco

您也可以使用它来匹配任何其他字母数字化合物。

【讨论】:

  • 太棒了!效果很棒。非常感谢!
猜你喜欢
  • 2017-01-25
  • 1970-01-01
  • 2016-03-08
  • 2013-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多