The pattern for hex numbers does not consider digits 'a' ... 'f'. Try this:
[-]?[0][xX][0-9a-fA-F]+ {cout << yytext << " Number" << endl; }
Further observations:
- The vertical bar in
[x|X]
is probably wrong. Otherwise, this would also work: 0|a98h
.
- The 'h' at end of sample is not matched. (This may or may not be intended.)
An alternative approach could be this (test-hex.l
):
%{
#include <iostream>
using namespace std;
%}
%option caseless
%%
[-]?[0][x][0-9a-f]+ {cout << yytext << " Number" << endl; }
%%
int main(int argc, char **argv) { return yylex(); }
int yywrap() { return 1; }
Compiled and tested with flex and gcc on cygwin:
$ flex -V
flex 2.6.3
$ flex -otest-hex.cc test-hex.l ; g++ -o test-hex test-hex.cc
$ echo '0xa98h' | ./test-hex
0xa98 Number
h
There is no pattern matching h
. This is printed because lex/flex generate a default rule to echo everything what is not matched to standard output.