2

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. Assume that each simple expression like above would evaluate to a true/false. My regex for this would be

(?P<lhs>[A-Z][a-zA-Z]*Property):{0,1}(?P<lhsPrimitiveValue>[A-Za-z0-9]*)\ *(?P<operator>[><=]|!=|<=|>=|in|not_in)\ *(?P<rhs>[A-Z][a-zA-Z]*Property):{0,1}(?P<rhsPrimitiveValue>[A-Za-z0-9]*)

lhs = Left Hand Side
rhs = Right Hand side.

Now, I want to combine number of simple expressions into compound expressions via logical operators And/Or(lets assume Not is out of context).

So, a valid compound expression would be

SimpleExpression1 and SimpleExpression2 or SimpleExpression3 where each simple expression inturn would match the above regex.

I want my regex group to return ('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. But couldn't able to expand further.

Mohan
  • 1,850
  • 1
  • 19
  • 42

1 Answers1

3

Maybe you want to try re.split, see also this question.

re.split('(and|or)', 'this and this or that')
Out[18]: ['this ', 'and', ' this ', 'or', ' that']
Community
  • 1
  • 1
maxymoo
  • 35,286
  • 11
  • 92
  • 119
  • Ha ha, that actually would work for my problem :-) Are there any regex solns available ? Mainly I am interested to see what I've missed. – Mohan Jun 11 '15 at 03:49
  • hmm not sure, to be honest i don't really like complicated regex's , i find them so hard to read and maintain, i'd much prefer a simpler solution with readable steps. – maxymoo Jun 11 '15 at 03:50