String split using multiple delimiters in java

荒凉一梦 提交于 2019-12-23 12:32:33

问题


I am working on a data mining algorithm where I need to tokenize the string using multiple words. I have a separate file which contain all the stopwords. What I need to do is to tokenize the input string by any of the word (stopword) working as delimiter. For eg.
If the file contains stopwords as
a
is
and
of
that

and the input string comes to be
"a computer cluster consists of a set of loosely connected computers that work together"
the output comes to be
computer cluster consists
set
loosely connected computers
work together

Checking string against all the stopwords recursively would be very time consuming? Is there any good method for this?


回答1:


Construct a regular expression of the form

delim1|delim2|delim3

then use String's split() method to split the text by any of the delimiters.

In order to construct the regexp, read each delimiter, and pass it to Pattern.quote before appending to the regex that you build. This would let your delimiters use regex metacharacters as well.



来源:https://stackoverflow.com/questions/13223183/string-split-using-multiple-delimiters-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!