title: Handling String Literals which End in an Escaped Quote in ANTLR4
body: <p>How do I write a lexer rule to match a String literal which does not end in an escaped quote? </p>
  
  <p>Here's my grammar:</p>
  
  <pre><code>lexer grammar StringLexer;
  
  // from The Definitive ANTLR 4 Reference
  STRING: '"' (ESC|.)*? '"';
  fragment ESC : '\\"' | '\\\\' ;
  </code></pre>
  
  <p>Here's my java block:</p>
  
  <pre><code>String s = "\"\\\""; // looks like "\"
  StringLexer lexer = new StringLexer(new ANTLRInputStream(s)); 
  
  Token t = lexer.nextToken();
  
  if (t.getType() == StringLexer.STRING) {
      System.out.println("Saw a String");
  }
  else {
      System.out.println("Nope");
  }
  </code></pre>
  
  <p>This outputs <code>Saw a String</code>. Should <code>"\"</code> really match <code>STRING</code>?</p>
  
  <p><b>Edit:</b> Both 280Z28 and Bart's solutions are great solutions, unfortunately I can only accept one.</p>