Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANTLRv4: How to read double quote escaped double quotes in string?

Tags:

antlr4

In ANTLR v4, how do we parse this kind of string with double quote escaped double quotes like in VBA?

for text:

"some string with ""john doe"" in it"

the goal would be to identify the string: some string with "john doe" in it

And is it possible to rewrite it to turn double double quotes in single double quotes? "" -> "?

like image 860
JayDee Avatar asked Jul 27 '13 12:07

JayDee


People also ask

How do you escape double quotes in double quotes?

If you use single quotes to create a string, you can not use single quotes within that string without escaping them using a backslash ( \ ). The same theory applies to double quotes, and you have to use a backslash to escape any double quotes inside double quotes.

How do you escape a double quote in query string?

Double quotes characters can be escaped with backslash( \ ) in java. You can find complete list of String escapes over this java doc.

How do you handle double quotes in a string?

If you need to use the double quote inside the string, you can use the backslash character. Notice how the backslash in the second line is used to escape the double quote characters. And the single quote can be used without a backslash.

How do you escape quotation marks in a string?

' You can put a backslash character followed by a quote ( \" or \' ). This is called an escape sequence and Python will remove the backslash, and put just the quote in the string.


1 Answers

Like this:

STRING
 : '"' (~[\r\n"] | '""')* '"'
 ;

where ~[\r\n"] | '""' means:

~[\r\n"]    # any char other than '\r', '\n' and double quotes
|           # OR
'""'        # two successive double quotes

And is it possible to rewrite it to turn double double quotes in single double quotes?

Not without embedding custom code. In Java that could look like:

STRING
 : '"' (~[\r\n"] | '""')* '"' 
   {
     String s = getText();
     s = s.substring(1, s.length() - 1); // strip the leading and trailing quotes
     s = s.replace("\"\"", "\""); // replace all double quotes with single quotes
     setText(s);
   }
 ;
like image 140
Bart Kiers Avatar answered Sep 30 '22 17:09

Bart Kiers