Reputation: 11
I'm trying to run some simple regex in Flex but I cannot get the operator "^" to work as a negation. This is my code:`
%%
[^abc] printf("rule triggered\n");
.
\n
It should just match any character that is not "a", "b" or "c", but instead I get a: "warning, rule cannot be matched".
The other uses for "^" seem to work fine, ^a matches any string that starts with "a" and [a^b] matches any string that contains "a" or "b". Also, strangely enough the rule [^a] works as expected if preceded by something else, such as:
%%
b+[^a] printf("rule triggered\n");
.
\n
This matches a string that begins with any number of "b"s that are not followed by an "a" and seems to work correctly. But it won't work if I allow zero occurences of "b" as in:
%%
b*[^a] printf("rule triggered\n");
.
\n
This gives the same "warning, rule cannot be matched" and sure enough, doesn't work. Kinda stumped here.
ps. I'm using Flex in cycgwin if that matters (it should be compatible with all Lex syntax).
`
Upvotes: 1
Views: 524
Reputation: 241931
It's the rule \n
that can't match anything. It can't match anything because a newline character is not an a
, a b
nor a c
, so it will always match the first rule and thus the third rule cannot every match.
Upvotes: 1