如何在java中的String中查找整个单词 [英] How to find a whole word in a String in java

查看:1316
本文介绍了如何在java中的String中查找整个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a String that I have to parse for different keywords. For example, I have the String:

我会在123woods见到你

"I will come and meet you at the 123woods"

我的关键字是

'123woods'
'woods'

'123woods' 'woods'

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

I should report whenever I have a match and where. Multiple occurrences should also be accounted for. However, for this one, I should get a match only on 123woods, not on woods. This eliminates using String.contains() method. Also, I should be able to have a list/set of keywords and check at the same time for their occurrence. In this example, if I have '123woods' and 'come', I should get two occurrences. Method execution should be somewhat fast on large texts.

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

My idea is to use StringTokenizer but I am unsure if it will perform well. Any suggestions?

推荐答案

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

The example below is based on your comments. It uses a List of keywords, which will be searched in a given String using word boundaries. It uses StringUtils from Apache Commons Lang to build the regular expression and print the matched groups.

String text = "I will come and meet you at the woods 123woods and all the woods";

List<String> tokens = new ArrayList<String>();
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中的高性能模式匹配算法。

If you are looking for more performance, you could have a look at StringSearch: high-performance pattern matching algorithms in Java.

这篇关于如何在java中的String中查找整个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆