Trouble escaping a period inside String.matches() method

user1830797 picture user1830797 · Nov 28, 2012 · Viewed 7.6k times · Source

I'm getting a compilation error when I use this regular expression inside the String class's matches() method. Anyone know what I'm doing wrong here? Thanks

String email = "[email protected]";
System.out.println(email.matches("^(.+@.+\.\\w{2,4})$"));

Answer

PermGenError picture PermGenError · Nov 28, 2012

escape period . with two backslashes \\. valid escape sequences supported in java are \b \t \n \f \r \" \' \\

System.out.println(email.matches("^(.+@.+\\.\\w{2,4})$"));

OR:

Enclose it within \\Q and \\E

\\Q.\\E