I have the ff. delimeters ![,?.\_'@+]
in my
str.split("![,?.\_'@+]")
. But, it cause an error when the program is compiled which states that \_
is an illegal escape character. I have tried to remove it and just pass ![,?.'@+]
instead, however string was not being split even if it contains ?, @ and .
character.

- 607,720
- 39
- 448
- 563

- 33
- 4
-
Escape the backslash to match backslash, `str.split("![,?.\\_'@+]")` – Tushar Jan 29 '16 at 09:07
-
2http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java Maybe the accepted answer can help you – TheTanic Jan 29 '16 at 09:08
-
There is nothing in [*How to split a string in Java*](http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) about how to split a string using a character class with *multiple* symbols. `Pattern.quote` cannot be used here. – Wiktor Stribiżew Jan 29 '16 at 09:24
-
Well, this one - [*Splitting string using multiple delimiters Java*](http://stackoverflow.com/questions/15774500/splitting-string-using-multiple-delimiters-java) - is similar, but it does not explain that in Java, the `[` symbol **must be escaped** inside a character class. – Wiktor Stribiżew Jan 29 '16 at 09:35
1 Answers
You need to put the symbols into a character class and escape [
, ]
and \
there:
str.split("[!\\[,?.\\\\_'@+\\]]")
The [!\\[,?.\\\\_'@+\\]]
character class matches a single character from the set you specified (!
, [
, ,
, ?
, .
, \
, _
, '
, @
, +
, or ]
).
Note that _
is not a special regex metacharacter and does not have to be (and should not be) escaped. If you need to also add \
character to the character class, you need to use 4 backslashes to match a literal \
in the input.
Note that whenever you need to add a hyphen to this regex, you should put it right at the beginning or the end of the character class. Please refer to Splitting string using multiple delimiters Java that dwells on the problem of how to match a hyphen inside a character class in a Java regex.
If you want to get rid of any empty elements in the resulting array, replace all the split characters at the start of the string, and apply a +
(one or more occurrences) quantifier to the character class (here is an updated answer with a whitespace added to the character class):
str.replaceAll("^[!\\[,?.\\\\_'@+\\]\\s]+, "").split("[!\\[,?.\\\\_'@+\\]\\s]+")

- 1
- 1

- 607,720
- 39
- 448
- 563
-
thanks! it works! But just another question, I added `\\s` so it is now `str.split("[!\\[,?.\\\\_'@+\\]\\s]")` but for some string like `sample, code! exec` the split returns ['sample', ' ', 'code', ' ', 'exec']. What will I gonna do to get rid of the extra space? The problem occurs when the space is next to a special character. – Jofel Bayron Jan 29 '16 at 09:41
-
That is not a big problem, just add a `+` quantifier to match one or more split characters: `str.split("[!\\[,?.\\\\_'@+\\]\\s]+")`. If you want to get rid of leading empty elements, use a replace before splitting: `str.replaceAll("^[!\\[,?.\\\\_'@+\\]\\s]+, "").split("[!\\[,?.\\\\_'@+\\]\\s]+")` – Wiktor Stribiżew Jan 29 '16 at 09:48