tags:

views:

99

answers:

1

I read from a lot of webpage (for example: http://www.wellho.net/regex/java.html), they all mentioned that \s could represent any space charactor. But when I use \s in Java, it is not an eligible expression.

Anyone know the reason?

+8  A: 

Backslashes inside strings need to be quoted in order to work.

For example, the following works fine:

public class testprog {
    public static void main(String args[]) {
        String s = "Hello there";
        System.out.println (s.matches(".*\\s.*"));
    }
}

outputting:

true

If you use a string like "\s", you should get an error along the lines of:

Invalid escape sequence - valid ones are  \b  \t  \n  \f  \r  \"  \'  \\

from your compiler since \s is not a valid escape sequence (for strings, I mean, not regexes).

paxdiablo