Regex for String with possible escape characters <p>I had asked this question some times back here <a href="http://stackoverflow.com/questions/9423563/regular-expression-that-does-not-contain-quote-but-can-contain-escaped-quote">Regular expression that does not contain quote but can contain escaped quote</a> and got the response, but somehow i am not able to make it work in Java. </p> <p>Basically i need to write a regular expression that matches a valid string beginning and ending with quotes, and can have quotes in between provided they are escaped.</p> <p>In the below code, i essentially want to match all the three strings and print true, but cannot.</p> <p>What should be the correct regex?</p> <p>Thanks</p> <pre><code>public static void main(String[] args) { String[] arr = new String[] { "\"tuco\"", "\"tuco \" ABC\"", "\"tuco \" ABC \" DEF\"" }; Pattern pattern = Pattern.compile("\"(?:[^\"\\\\]+|\\\\.)*\""); for (String str : arr) { Matcher matcher = pattern.matcher(str); System.out.println(matcher.matches()); } } </code></pre>