Rabodaber
Rabodaber

Reputation: 183

Getting non-greedy sequence as a string with ANTLR

I have a problem with getting sequence as a string. I have a file with strings like:

{TEXT="<div itemprop=\"content\"><div>some text</div>"}

I want to get and use text that exactly between first and last quotes. First i tried:

parse : line+;
line : '{TEXT="' SEQUENCE '"}' {System.out.println($SEQUENCE.getText())};
SEQUENCE : .+?;

But it failed, SEQUENCE get only one symbol in that way. I tried:

parse : line+;
line : '{TEXT="' (a+=SEQUENCE)*? '"}' {System.out.println($a.getText())};
SEQUENCE : .;

And I got List of Tokens, so i can't use getText.

Upvotes: 0

Views: 95

Answers (1)

Andy
Andy

Reputation: 644

if you want to do it in this way, you can do it like this:

grammar Sequence;

parse : line+;
line : '{TEXT="' a=sequence '"}' {System.out.println(((LineContext)_localctx).a.getText());};
sequence : .+?;

ANY:.;

But there also other mechanisms in ANTLR4 like listeners and visitors.

Upvotes: 1

Related Questions