Search code examples
regexjflex

How to match something not defined


if I have defined something like that

COMMAND         = "HI" | "HOW" | "ARE" | "YOU" 

How can i say "if u match something that is not a COMMAND"?..

I tried with this one

[^COMMAND]

But didn't work..


Solution

  • As far as I can tell this is not possible with (current) JFlex.

    We would need an effective tempered negative lookahead: ((?!bad).)*

    There are two ways to do a negative lookahead in JFlex:

    • negation in the lookahead: x / !(y [^]*) (match x if not followed by y in the lookahead).
    • lookahead with negated elements: x / [^y]|y[^z] (match if x is followed by something that is !a or a!b.

    Otherwise, you may get some ideas from this answer (specifically the lookaround alternatives): https://stackoverflow.com/a/37988661/8291949