When I tried to use regex in flex as following to define an int type:
当我尝试在flex中使用正则表达式时,如下定义一个int类型:
int (?<!\w)(([1-9]\d*)|0)(?!\w)
I meant to make this invalid:
我打算让这个无效:
int a = 123;
int b = 123f; //the '123' should not filtered as an int type
However, I got this:
但是,我得到了这个:
bad character: <
bad character: !
bad character: \
...
What's more, it seems that the ?
in the regex was ignored. I got confused. Does flex not support the lookahead assertion (?<=xxx)
or (?<!xxx)
?
更何况,似乎是?在正则表达式被忽略了。我很困惑。 flex不支持前瞻断言(?<= xxx)或(?<!xxx)?
I am new in flex, I really need some help
我是flex的新手,我真的需要一些帮助
1 个解决方案
#1
8
That's correct. Flex does not support negative lookahead assertions. It also does not support \w
or \d
, although it does allow posix-style character classes ([[:alpha:]]
, [[:digit:]]
, [[:alnum:]]
, etc.)
那是对的。 Flex不支持负前瞻断言。它也不支持\ w或\ d,虽然它允许posix样式的字符类([[:alpha:]],[[:digit:]],[[:alnum:]]等。)
Flex regular expressions are quite different from javascript-like or perl/python-like "regular" expressions. For one thing, flex's regular expressions are really regular.
Flex正则表达式与类似javascript或perl / python的“常规”表达式完全不同。首先,flex的正则表达式非常规则。
A complete list of the syntaxes flex allows is in the flex manual. Anything not described in that section of the manual is not implemented by flex.
flex flex允许的语法完整列表在flex手册中。 flex中未实现本手册中未描述的任何内容。
There is very little point using "lookbehind" with flex, because flex always matches the longest token at the current input point. It does not search the input for a pattern.
使用flex的“lookbehind”几乎没有意义,因为flex总是匹配当前输入点的最长令牌。它不会在输入中搜索模式。
Flex does implement a limited form of positive lookahead, using the /
operator (which is not part of any regular expression library I know of.) You could use that to only match a sequence of digits not immediately followed by a letter:
Flex使用/运算符(它不是我所知道的任何正则表达式库的一部分)实现有限形式的正向前瞻。你可以使用它来匹配一个数字序列,而不是紧跟一个字母:
[[:digit:]]+/[^[:alpha:]]
But you'll then need some pattern which does match the sequence of digits followed by an alphabetic character, because flex does not search for a matching token.
但是,您将需要一些与数字序列匹配的模式,后跟字母字符,因为flex不会搜索匹配的标记。
#1
8
That's correct. Flex does not support negative lookahead assertions. It also does not support \w
or \d
, although it does allow posix-style character classes ([[:alpha:]]
, [[:digit:]]
, [[:alnum:]]
, etc.)
那是对的。 Flex不支持负前瞻断言。它也不支持\ w或\ d,虽然它允许posix样式的字符类([[:alpha:]],[[:digit:]],[[:alnum:]]等。)
Flex regular expressions are quite different from javascript-like or perl/python-like "regular" expressions. For one thing, flex's regular expressions are really regular.
Flex正则表达式与类似javascript或perl / python的“常规”表达式完全不同。首先,flex的正则表达式非常规则。
A complete list of the syntaxes flex allows is in the flex manual. Anything not described in that section of the manual is not implemented by flex.
flex flex允许的语法完整列表在flex手册中。 flex中未实现本手册中未描述的任何内容。
There is very little point using "lookbehind" with flex, because flex always matches the longest token at the current input point. It does not search the input for a pattern.
使用flex的“lookbehind”几乎没有意义,因为flex总是匹配当前输入点的最长令牌。它不会在输入中搜索模式。
Flex does implement a limited form of positive lookahead, using the /
operator (which is not part of any regular expression library I know of.) You could use that to only match a sequence of digits not immediately followed by a letter:
Flex使用/运算符(它不是我所知道的任何正则表达式库的一部分)实现有限形式的正向前瞻。你可以使用它来匹配一个数字序列,而不是紧跟一个字母:
[[:digit:]]+/[^[:alpha:]]
But you'll then need some pattern which does match the sequence of digits followed by an alphabetic character, because flex does not search for a matching token.
但是,您将需要一些与数字序列匹配的模式,后跟字母字符,因为flex不会搜索匹配的标记。