Search code examples
javaregexcharacter-class

How to create a character class of the following set


+ - * / % < > = ! & ^ | ? :

I've tried:

[+-*/%<>=!&^|?:]

But I think some of them will need to be escaped. How can I tell which ones?


Solution

  • You'll need to escape the - as otherwise it'd be interpreted as a character range between + and *, which isn't valid:

    [+\-*/%<>=!&^|?:]
    

    The other metacharacters, as far as I know, are taken literally in a character class.