Swaroop Nagendra
Swaroop Nagendra

Reputation: 605

Regexp in lex. Why does flex behave this way

Consider a simple integer digit identifying expression like this:

[0-9]+ printf("Integer");

Now if i give 123 as an input it returns Integer, fair enough. Now if I give s123 as the input it prints out sInteger. The unmatched s is being printed by default ECHO that's cool with me. But why is Integer also printed. Shouldn't lex return just s? My input is considered as a whole string right? I mean s123 is considered as a 1 full input?. As soon as s is encountered which does not match [0-9]+ so it should just echo default unmatched value s123 but why sInteger?

Upvotes: 0

Views: 38

Answers (1)

Vince
Vince

Reputation: 1527

The string s123 is being matched by the regex [0-9]+. If you want to match strings which consist of only integers, you should try ^[0-9]+$.

Upvotes: 1

Related Questions