How to use regexp capture groups with JFlex?

Although this question relates to JFlex, it probably refers to other scanner generators such as lex, flex.

If I have a rule, how can I create a capture group in part of this rule and use the result of this captured group as an argument for the code invoked by the rule?

For example, suppose I had a simple rule to match an SGML tag:

"<"[a-zA-Z]+">"    {return new Token(Type.OPEN_TAG);}

How could I capture part of the inner character ([a-zA-Z] +) and use it as an argument in my token constructor?

Edit: I know that I could just use yytext () to get all the consistent value, and then separate the parts elsewhere in the code, but it looks like this will make things more complicated than they should be.

+3
source share
1 answer

Scanner generators usually do not support capture groups, and frankly, I have never seen the real need for them in a scanner generator. Most of the things you would normally want capture groups for other RegEx engines to handle better in the parser or in a simple piece of code in action.

Perhaps something like the following.

"<"[a-zA-Z]+">"    {
                     String matchedText = yytext();
                     String label = matchedText.substring(1, matchedText.length() - 1);
                     return new Token(Type.OPEN_TAG, label);
                   }

, , . JFlex, , , - flex, , /, , .

+1

Source: https://habr.com/ru/post/1768879/


All Articles