0

I need help to understand regex. For some reason, i'v read lots of tutorials and lots of questions here on sof, and cannot manage to understand it. I want to use String.split function like in here: Java string.split - by multiple character delimiter. But i cant understand this part:

String[] parts = input.split("[,\\s\\-:\\?]");

Lets supose i want to make a split with this following delimiters: " " (space), "." (full stop), "," (coma), "!", "»", "«", etc. How can i get this with regex? I want understand what this characters in here: ("[,\s\-:\?]") are. What does "\s", why is it needed.

Community
  • 1
  • 1
recoInrelax
  • 697
  • 2
  • 16
  • 33
  • 1
    I recommend this website for testing your regular expressions: [RegExr](http://gskinner.com/RegExr/). It also offers explanations for the pattern itself. – Hamed Oct 01 '11 at 15:46

1 Answers1

2

\s is "any single whitespace character". So it represents a space, newline, tab, or other such.

Note that the regex could actually be written more compactly in this case:

String[] parts = input.split("[,\\s:?-]");

(? doesn't need to be escaped when inside [], and - doesn't either if it's the last thing.)

For more details about character classes, see http://www.regular-expressions.info/charclass.html

Amber
  • 507,862
  • 82
  • 626
  • 550