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.
\sis “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:
(
?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