[regex] What is the Regular Expression For "Not Whitespace and Not a hyphen"

I tried this but it doesn't work :

[^\s-]

Any Ideas?

This question is related to regex

The answer is


In Java:

    String regex = "[^-\\s]";

    System.out.println("-".matches(regex)); // prints "false"
    System.out.println(" ".matches(regex)); // prints "false"
    System.out.println("+".matches(regex)); // prints "true"

The regex [^-\s] works as expected. [^\s-] also works.

See also


It can be done much easier:

\S which equals [^ \t\r\n\v\f]


Try [^- ], \s will match 5 other characters beside the space (like tab, newline, formfeed, carriage return).


Which programming language are you using? May be you just need to escape the backslash like "[^\\s-]"