16

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
Büşra GÜL
  • 323
  • 1
  • 4
  • 16

2 Answers2

13

Try with

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

Also

Use String.split() with multiple delimiters

Community
  • 1
  • 1
AMB
  • 995
  • 1
  • 12
  • 26
  • You seems to know that `split` takes regex, but forgot that some characters in regex are special and may require escaping. In current form this code will throw PatternSyntaxException since it is not properly created regex. – Pshemo Dec 21 '16 at 19:07
  • Fixed now @Pshemo – AMB Dec 22 '16 at 09:54
3

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.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
aleb2000
  • 452
  • 4
  • 10