Oliver
Oliver

Reputation: 4173

How to strip quotes from a string

I wrote a simple rule to match a string in an ANTLR grammar:

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

Actually I need the content of the string and not the quotes, which are only required to match a string.

I found a solution for ANTLR 3, which is published in the ANTLR wiki. But I would like to know if there is a solution to achive the same without custom code.

Upvotes: 1

Views: 1819

Answers (1)

Wouter
Wouter

Reputation: 4016

This should work:

STRING
   :   '"' (ESC | ~["\\])* '"'
   {setText(getText().substring(1, getText().length()-1));}
   ;

It simple removes the first and last character from the string.

Taken from https://theantlrguy.atlassian.net/wiki/spaces/ANTLR3/pages/2687006/How+do+I+strip+quotes

Upvotes: 2

Related Questions