I am looking for a regular expression in python to match a logical expression.
I want to match string NOT search string.
I just want to match the two literals between a logical operator(AND|OR) which are two different words separated by space.
Example:
The following conditions should match:
- (abc AND xyz)
- (abc AND 123)
- (abc AND 123.456)
- (123 AND 123.456)
- (.001 AND 1)
Same with OR operator
- (abc OR xyz)
- (abc OR 123)
- (abc OR 123.456)
- (123 OR 123.456)
- (.001 OR 1)
The following conditions should NOT match:
- (AND AND AND)
- (AND AND abc)
- (123 AND AND)
- (OR AND OR)
- (AND OR OR)
I tried the following without any success, ('AND AND abc') still matches... ('abc AND AND') doesn't match though.
^((?!AND$|OR$)\w+|\d*\.\d+|\d+)\s+(AND|OR)\s+((?!AND$|OR$)\w+|\d*\.\d+|\d+)$
code:
p=re.compile(r'(^((?!AND$|OR$)\w+|\d*\.\d+|\d+)\s+(AND|OR)\s+((?!AND$|OR$)\w+|\d*\.\d+|\d+)$)')
p.match('AND AND abc')
Thanks in advance for all your help!