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.

1
  • 1
    I recommend this website for testing your regular expressions: RegExr. It also offers explanations for the pattern itself. Commented Oct 1, 2011 at 15:46

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.