在 Java 中拆分多个分隔符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41247737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:15:21  来源:igfitidea点击:

Split multiple delimiters in Java

javasplitcountdelimiterfrequency

提问by Bü?ra GüL

How I can split the sentences with respect to the delimiters in the string and count the frequency of words ?

如何根据字符串中的分隔符拆分句子并计算单词的频率?

 String delimiters = "\t,;.?!-:@[](){}_*/";

My text file is:

我的文本文件是:

Billy_Reeves

Smorz

Nationalist_Left_-_Youth

Ancient_Greek_units_of_measurement

Jiuting_(Shanghai_Metro)

Blodgett,_MO

Baekjeong

Matt_Brinkman

National_Vietnam_Veterans_Art_Museum

采纳答案by AMB

Try with

试试

split("\t|,|;|\.|\?|!|-|:|@|\[|\]|\(|\)|\{|\}|_|\*|/");

Also

Use String.split() with multiple delimiters

使用带有多个分隔符的 String.split()

回答by aleb2000

The split method takes as argument a regular expression so, to use multiple delimiters, you need to input a regular expression separated by the OR regex operator or using a character class (only if the delimiters are single characters).

split 方法将正则表达式作为参数,因此,要使用多个分隔符,您需要输入由 OR regex 运算符分隔的正则表达式或使用字符类(仅当分隔符为单个字符时)。

Using the OR operator:

使用 OR 运算符:

String delimiters = "\t|,|;|\.|\?|!|-|:|@|\[|\]|\(|\)|\{|\}|_|\*|/";

Using the character class:

使用字符类:

String delimiters = "[-\t,;.?!:@\[\](){}_*/]";

As you can see some of the characters must be escaped as they are regex metacharacters.

如您所见,某些字符必须转义,因为它们是正则表达式元字符。