regexbnfjflex

jflex regular expression any number except zero


I'm new in JFlex and I ran into a problem with regular expression.

I'm trying to write in .flex file regex that will recognize any number except zero.

The thing is, when I tried my regex in .bnf file everything works fine in live-preview, but when i'm trying to use .flex generated class - nothing worked.

This is my regex in .bnf file that work's well:

{
      tokens = [
         NUMBER = 'regexp:^[1-9]\d*'
      ]
}

But in .flex file this regex is not working:

NUMBER = ^[1-9]\d*  // Compile error here. <expression> expected, got '^'
NUMBER = [^[1-9]\d*]  // Compiles, but not working.
NUMBER = \^[1-9]\d*\  // Compiles, but not working.

What am I doing wrong?


Solution

  • You may use

    NUMBER = [1-9][0-9]*
    

    This will match a digit from 1 to 9 and then any zero or more digits.

    The ^ start of string anchor seems to be not supported here.