用于分割BNF语法符号的正则表达式

时间:2014-12-23 16:03:04

标签: python regex python-2.7 bnf

我需要迭代形式的生产规则的符号:

e.g: 输入

<relational operator> ::= = | <> | < | <= | >= | > | in
<next constant definition> ::= <empty> | <next constant definition> ; <constant definition>

所以我需要派生一个正则表达式来分割文本。这是我到目前为止所拥有的

(?:\s|^|\s<|^<)(?:.*?)(?:\s|$|\s>|>$)

问题是re.findall()无法产生我想要的输出

预期输出为:

[<relational operator>, ::=, =, |, <>, |, <, |, <=, |, >=, |, >, |, in]
[<next constant definition>, ::=, <empty>, |, <next constant definition>, ;, <constant definition>]

1 个答案:

答案 0 :(得分:2)

如何使用像<\w+(?:\s+\w+)*>|\S+

这样的简单内容
   < \w+ 
   (?: \s+ \w+ )*
   >
|  
   \S+