1

I am trying to find a way to achieve recognizing a token that has only one underscore and it is not allowded to start or finish with an underscore.Something like:

abc_def:VALID TOKEN
_abc:NOT VALID TOKEN
abc_:NOT VALID TOKEN

I cant understand how i can allow only one underscore when there are characters after the underscore that will be repeating.Is this possible with a regex in flex/lex?

4

2 回答 2

1

演示

正则表达式:^[^_]+_[^_]+$

解释:

  1. [^_]+-> 任何不是_至少一个字符的。
  2. _-> 下划线
  3. 与 1 相同
  4. ^$是分别分隔字符串开头和结尾的锚点。

如果您想将正则表达式限制为 3 个字符,请使用{3}而不是加号。

于 2013-01-05T23:23:36.477 回答
-1

Python中的一个例子:

In [1]: import re

In [2]: re.findall(r'\b[^\s_]+_[^\s_]+\b', 'abc_def, abc__def, __abc_def, abc_def__')
Out[2]: ['abc_def']
于 2013-01-05T23:30:25.153 回答