In a java regex, how can I get a character class e.g. [a-z] to match a - minus sign?

java, regex

Solution

Don't put the minus sign between characters.

"[a-z-]"

Problem

``` Pattern pattern = Pattern.compile("^[a-z]+$"); String string = "abc-def"; assertTrue( pattern.matcher(string).matches() ); // obviously fails ``` Is it possible to have the character class match a "-" ?

Original source