如何在Java中的字符串中查找整个单词? [英] How to find a whole word in a String in Java?

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

问题描述

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

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'
'树林'

'123woods'
'woods'

每当我有比赛和地点时,我都应该报告.还应考虑多次发生.

I should report whenever I have a match and where. Multiple occurrences should also be accounted for.

然而,对于这个,我应该只在 '123woods' 上匹配,而不是在 'woods' 上.这消除了使用 String.contains() 方法.此外,我应该能够拥有一个关键字列表/一组关键字并同时检查它们的出现.在这个例子中,如果我有 '123woods''come',我应该得到两次出现.方法在大文本上的执行应该有点快.

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中的字符串中查找整个单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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