[英]Python regex for logical operators
I am developing a rule system which can accept relational and logical operators. 我正在开发一个可以接受关系和逻辑运算符的规则系统。
A simple expression in the rule system could be as simple as 规则系统中的简单表达式可能像
EmployeeAgeProperty >= IntegerProperty:27
where I define what would substitute the EmployeeHireDateProperty
and my rulesystem will evaluate that. 我在其中定义将替代
EmployeeHireDateProperty
而我的规则系统将对此进行评估。 Assume that each simple expression like above would evaluate to a true/false. 假设上面的每个简单表达式的求值结果为true / false。 My regex for this would be
我的正则表达式是
(?P<lhs>[AZ][a-zA-Z]*Property):{0,1}(?P<lhsPrimitiveValue>[A-Za-z0-9]*)\\ *(?P<operator>[><=]|!=|<=|>=|in|not_in)\\ *(?P<rhs>[AZ][a-zA-Z]*Property):{0,1}(?P<rhsPrimitiveValue>[A-Za-z0-9]*)
lhs = Left Hand Side lhs =左手边
rhs = Right Hand side. rhs =右侧。
Now, I want to combine number of simple expressions into compound expressions via logical operators And/Or(lets assume Not is out of context). 现在,我想通过逻辑运算符And / Or将多个简单表达式组合为复合表达式(假设Not不在上下文中)。
So, a valid compound expression would be 因此,有效的复合表达式为
SimpleExpression1 and SimpleExpression2 or SimpleExpression3
where each simple expression inturn would match the above regex. SimpleExpression1 and SimpleExpression2 or SimpleExpression3
,其中每个简单表达式反过来都将与上述正则表达式匹配。
I want my regex group to return ('SimpleExpression1', 'and', SimpleExpression2', 'or', 'SimpleExpression3')
我希望我的正则表达式组返回
('SimpleExpression1', 'and', SimpleExpression2', 'or', 'SimpleExpression3')
I could comeup with something like this 我可以想到这样的事情
([\\w\\d!=< ]*)\\ *(and|or)\\ *([\\w\\d!=< ]*)
which would match a single compound expression with one and/or. ([\\w\\d!=< ]*)\\ *(and|or)\\ *([\\w\\d!=< ]*)
,它将单个化合物表达式与一个和/或匹配。 But couldn't able to expand further. 但是无法进一步扩展。
Maybe you want to try re.split
, see also this question. 也许您想尝试
re.split
,另请参阅此问题。
re.split('(and|or)', 'this and this or that')
Out[18]: ['this ', 'and', ' this ', 'or', ' that']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.