With reference to below question - String.replaceAll single backslashes with double backslashes
I wrote a test program, and I found that the result is true in both cases, whether I escape the backslash or not. This may be because - \t is a recognized Java String escape sequence. (Try \s and it would complain). - \t is taken as literal tab in the regex. I am somewhat unsure of the reasons.
Is there any general guideline about escaping regex in Java. I think using two backslashes is the correct approach.
I would still like to know your opinions.
public class TestDeleteMe {
public static void main(String args[]) {
System.out.println(System.currentTimeMillis());
String str1 = "a b"; //tab between a and b
//pattern - a and b with any number of spaces or tabs between
System.out.println("matches = " + str1.matches("^a[ \\t]*b$"));
System.out.println("matches = " + str1.matches("^a[ \t]*b$"));
}
}