Rolando
Rolando

Reputation: 62664

How to split on any non-word character with 0 or more trailing and leading whitespace?

As the question states, how to form a regular expression so that it "splits" on everything that is a non-word character with 0 or more trailing or leading whitespace?

So:

String a= Hello. My name is "Jello" What is your name?
a.split("expression")

Becomes...

[Hello, My, name, is, Jello, What, is, your, name]

Upvotes: 0

Views: 96

Answers (1)

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79847

You want

a.split("\\W+")

as described in the Javadoc for Pattern. - \W is any non-word character and + means "one or more".

Upvotes: 1

Related Questions