Scanner uses regular expression (regex) to describe delimiter. By default it is \p{javaWhitespace}+
which represents one or more (due to +
operator) whitespaces.
In regex to represent single character from set of characters we can use character class [...]
. But since [
and ]
in regex represents start and end of character class these characters are metacharacters (even inside character class). To treat them as literals we need to escape them first. We can do it by
- adding
\
(in string written as "\\"
) before them,
- or by placing them in
\Q...\E
which represents quote section (where all characters are considered as literals, not metacharacters).
So regex representing one of ( ) { } [ ] , !
characters can look like "[\\Q(){}[],!\\E]"
.
If you want to add support for standard delimiter you can combine this regex with \p{javaWhitespace}+
using OR operator which is |
.
So your code can look like:
yourScanner.useDelimiter("[\\Q(){}[],!\\E]|\\p{javaWhitespace}+");