How to find a whole word in a String in java -


i have string have parse different keywords. example, have string:

"i come , meet @ 123woods"

and keywords

'123woods' 'woods'

i should report whenever have match , where. multiple occurrences should accounted for. however, one, should match on 123woods, not on woods. eliminates using string.contains() method. also, should able have list/set of keywords , check @ same time occurrence. in example, if have '123woods' , 'come', should 2 occurrences. method execution should fast on large texts.

my idea use stringtokenizer unsure if perform well. suggestions?

the example below based on comments. uses list of keywords, searched in given string using word boundaries. uses stringutils apache commons lang build regular expression , print matched groups.

string text = "i come , meet @ woods 123woods , 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)); } 

if looking more performance, have @ stringsearch: high-performance pattern matching algorithms in java.


Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -