Split Multiple Delimiters in Java [Closed]

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
1

Try with

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

Also

Use String.split() with multiple delimiters

2

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).

Using the OR operator:

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.

0
David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest