如何在java中的String中查找整个单词

我有一个字符串,我必须解析不同的关键字。 例如,我有字符串:

“我会在123woods来见你”

我的关键字是

‘123woods”森林’

我应该在每次有比赛时报告。 还应考虑多次出现。 然而,对于这个,我应该只在123woods上获得一场比赛,而不是在树林上。 这消除了使用String.contains()方法。 此外,我应该能够有一个列表/一组关键字,并同时检查它们的发生。 在这个例子中,如果我有’123woods’和’come’,我应该得到两次。 方法执行在大文本上应该有点快。

我的想法是使用StringTokenizer,但我不确定它是否会表现良好。 有什么建议么?

以下示例基于您的评论。 它使用关键字列表,将使用字边界在给定的字符串中搜索。 它使用Apache Commons Lang的StringUtils构建正则表达式并打印匹配的组。

String text = "I will come and meet you at the woods 123woods and all the woods"; List tokens = new ArrayList(); tokens.add("123woods"); tokens.add("woods"); String patternString = "\\b(" + StringUtils.join(tokens, "|") + ")\\b"; Pattern pattern = Pattern.compile(patternString); Matcher matcher = pattern.matcher(text); while (matcher.find()) { System.out.println(matcher.group(1)); } 

如果您正在寻找更高的性能,可以查看StringSearch :Java中的高性能模式匹配算法。

使用正则表达式+单词边界作为其他人回答。

 "I will come and meet you at the 123woods".matches(".*\\b123woods\\b.*"); 

将是真的。

 "I will come and meet you at the 123woods".matches(".*\\bwoods\\b.*"); 

将是假的。

Arrays.asList(String.split(" ")).contains("xx")怎么样?

请参见String.split()以及如何测试数组是否包含特定值 。

希望这对你有用:

 String string = "I will come and meet you at the 123woods"; String keyword = "123woods"; Boolean found = Arrays.asList(string.split(" ")).contains(keyword); if(found){ System.out.println("Keyword matched the string"); } 

http://codigounico.blogspot.com/

尝试使用正则表达式匹配。 匹配“\ b123wood \ b”,\ b是分词。

有办法匹配 Android中的字符串中的精确单词

 String full = "Hello World. How are you ?"; String one = "Hell"; String two = "Hello"; String three = "are"; String four = "ar"; boolean is1 = isContainExactWord(full, one); boolean is2 = isContainExactWord(full, two); boolean is3 = isContainExactWord(full, three); boolean is4 = isContainExactWord(full, four); Log.i("Contains Result", is1+"-"+is2+"-"+is3+"-"+is4); Result: false-true-true-false 

匹配词的function:

 private boolean isContainExactWord(String fullString, String partWord){ String pattern = "\\b"+partWord+"\\b"; Pattern p=Pattern.compile(pattern); Matcher m=p.matcher(fullString); return m.find(); } 

完成

一个更简单的方法是使用split():

 String match = "123woods"; String text = "I will come and meet you at the 123woods"; String[] sentence = text.split(); for(String word: sentence) { if(word.equals(match)) return true; } return false; 

这是一种更简单,更不优雅的方式来做同样的事情而不使用令牌等。

解决方案似乎已被长期接受,但解决方案可以改进,所以如果有人有类似的问题:

这是多模式搜索算法的经典应用程序。

Java模式搜索(使用Matcher.find )没有资格这样做。 在java中优化搜索恰好一个关键字,搜索or-expression使用正在回溯不匹配的正则表达式非确定性自动机。 在更糟糕的情况下,文本的每个字符将被处理l次(其中l是模式长度的总和)。

单一模式搜索更好,但也不合格。 人们将不得不开始搜索每个关键字模式。 在更糟糕的情况下,文本的每个字符将被处理p次,其中p是模式的数量。

多模式搜索将仅处理文本的每个字符一次。 适合于这种搜索的算法将是Aho-Corasick,Wu-Manber或Set Backwards Oracle Matching。 这些可以在诸如Stringsearchalgorithms或byteseek之类的库中找到。

 // example with StringSearchAlgorithms AhoCorasick stringSearch = new AhoCorasick(asList("123woods", "woods")); CharProvider text = new StringCharProvider("I will come and meet you at the woods 123woods and all the woods", 0); StringFinder finder = stringSearch.createFinder(text); List all = finder.findAll(); 

您可以使用正则表达式。 使用Matcher和Pattern方法获得所需的输出

您还可以使用正则表达式匹配\ b标志(整个单词边界)。

要匹配“123woods”而不是“woods”,请在常规表达中使用primefaces分组。 需要注意的一点是,在单独匹配“123woods”的字符串中,它将匹配第一个“123woods”并退出而不是进一步搜索相同的字符串。

 \b(?>123woods|woods)\b 

它搜索123woods作为主要搜索,一旦匹配就退出搜索。

回顾原始问题,我们需要在给定的句子中找到一些给定的关键词,计算出现次数并了解其中的某些内容。 我不太明白“where”是什么意思(它是句子中的索引吗?),所以我会通过那个…我还在学习java,一步一步,所以我会看到在适当的时候到那个:-)

必须注意的是,常见句子(如原始问题中的那个)可以具有重复的关键字,因此搜索不仅可以询问给定关键字是否“存在”,并且如果确实存在则将其计为1。 可以有多个相同的。 例如:

 // Base sentence (added punctuation, to make it more interesting): String sentence = "Say that 123 of us will come by and meet you, " + "say, at the woods of 123woods."; // Split it (punctuation taken in consideration, as well): java.util.List strings = java.util.Arrays.asList(sentence.split(" |,|\\.")); // My keywords: java.util.ArrayList keywords = new java.util.ArrayList<>(); keywords.add("123woods"); keywords.add("come"); keywords.add("you"); keywords.add("say"); 

通过观察它,预期结果将是“Say”+“come”+“you”+“say”+“123woods”5,如果我们小写,则计算“说”两次。 如果我们不这样做,那么计数应该是4,“Say”被排除在外并且“说”包括在内。 精细。 我的建议是:

 // Set... ready...? int counter = 0; // Go! for(String s : strings) { // Asking if the sentence exists in the keywords, not the other // around, to find repeated keywords in the sentence. Boolean found = keywords.contains(s.toLowerCase()); if(found) { counter ++; System.out.println("Found: " + s); } } // Statistics: if (counter > 0) { System.out.println("In sentence: " + sentence + "\n" + "Count: " + counter); } 

结果是:

发现:说
发现:来
找到你
发现:说
发现:123woods
在句子中:假设我们中的123个人会来到123woods的树林中遇见你。
数:5

 public class FindTextInLine { String match = "123woods"; String text = "I will come and meet you at the 123woods"; public void findText () { if (text.contains(match)) { System.out.println("Keyword matched the string" ); } } }