tags:

views:

1107

answers:

4
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 "-" ?

+5  A: 

Escape the minus sign [a-z\-]

AlbertEin
+12  A: 

Don't put the minus sign between characters.

"[a-z-]"
Tomalak
This was downvoted, but it also works. I always just put the dash at the end.
harpo
It's working for me
daveb
I'm really not sure that this is standard regexp syntax. I don't doubt that it works, but it's almost certainly not best practice.
Daniel Spiewak
I beg to differ. The resulting character class is and works exactly the same. Escaping may or may not work depending on the regex dialect you use. In this case it's Java, who knows what it will be the next time. Putting the dash at the end (or the beginging) always works.
Tomalak
This *is* the correct syntax in every regexp dialect known to me. The minus sign does *not* have to be escaped if it's at the beginning or the end of the character class.
Konrad Rudolph
Escaping works everywhere except for implementations of POSIX regular expressions. According to the information I've dug up, it seems that this is standard syntax, but it still strikes me as ugly and accident prone. It's just bad language design that regexp even allows this sort of thing.
Daniel Spiewak
And, to make that clear: It is perfect standard syntax.See: http://www.regular-expressions.info/charclass.html (as one example of many)
Tomalak
Interesting you put it at the end. I put it at the beginning because I can imagine cases where having it at the end could be ambiguous (literal - and end of class or a run a characters ending with ]?), which isn't possible at the beginning of the class.
Dave Sherohman
It cannot be a sequence of characters ending in ']'. This would produce ambiguous syntax, so it is not allowed. The only place a closing square bracket may exist is right after the opening one ('[]a-z-]') or right after the negation ('[^]a-z-]').
Tomalak
+2  A: 

This works for me

   Pattern p = Pattern.compile("^[a-z\\-]+$");
   String line = "abc-def";
   Matcher matcher = p.matcher(line);
   System.out.println(matcher.matches());  // true
Michael Easter
Just curious, does it also work if your line is "abc\def"? It seems to me that you're matching both \ and - chars.
Parappa
+3  A: 

I'd rephrase the "don't put it between characters" a little more concretely.

Make the dash the first or last character in the character class. For example "[-a-z1-9]" matches lower-case characters, digits or dash.

John M