0

Tell me please how to make regular expression to separate words. Lets assume there is string

String s = "I have   dog, cat,    gold       fishes.    My cat   eats :      milk,    fish, etc.."

I need String array based on that string that looks like

 String[] words = s.split(regexp)

[I, have, dog, cat, gold, fishes, My, cat, eats, milk, fish, etc]

So regex must ignore whitespaces and punctuation(dots, commas, ?, !, )

Edison Miranda
  • 323
  • 1
  • 5
  • 15
  • 2
    It's a very simple task, if you read a basic tutorial about regex, you will find the answer in two minutes. An advice: using the find method is more easy. – Casimir et Hippolyte Nov 08 '14 at 16:50

1 Answers1

1

This should work:

String[] words = s.split("[\\s,.:]+");

To include all punctuations, use \p{Punct}:

String[] words = s.split("[\\s\\p{Punct}]+");
M A
  • 71,713
  • 13
  • 134
  • 174