简化AND OR布尔表达式 [英] simplifying AND OR Boolean Expression
问题描述
我的问题是变成一个看起来像这样的字符串.
My problem is turning a string that looks like this.
"a OR (b AND c)"
into
a OR bc
如果表达式类似于
"a AND (b OR c)"
then gives
ab OR ac
我无法使用REGEX匹配设计正确的循环集.问题的症结在于代码必须完全通用,因为我无法假设字符串模式将持续多长时间,或者OR AND在模式中的确切位置也将不会.
I can't able to design a correct set of loops using REGEX matching. The crux of the issue is that the code has to be completely general because I cannot assume how long the string pattern will be , nor exact places of OR AND in pattern will be.
- 安全性或((互联网或在线或无纸化且(银行*))和(手机或手机或电话或访问权)或在银行或交易中或在线或孟买或德里附近的NEAR/10琼脂或(在线或互联网) AND(银行)或非AND(苹果)不包括(mongo)
- security OR ((internet OR online OR paperless) AND (bank*)) AND (mobile OR cell OR phone OR access) OR easy OR online WITHIN bank OR transaction OR mumbai OR delhi NEAR/10 agar OR (online OR internet) AND (bank) OR not OR (apple) EXCLUDE (mongo)
如果我这样输入,它也会解决这种类型的表达式.
If i input like this ,it will solve this type of expression also.
推荐答案
Imo,您将需要在此处使用解析器,例如PLY
.您需要定义所有积木,然后可以构建语法树,使用该语法树可以执行任何您想做的事情.
一个例子可能是:
Imo, you will need to use a parser here, e.g. PLY
. You need to define all of your bricks and can then build a syntax tree with which you can do whatever you want.
An example could be:
import ply.lex as lex
# List of token names. This is always required
tokens = (
'VARIABLE',
'WHITESPACE',
'OR',
'AND',
'NOT',
'PAR_OPEN',
'PAR_CLOSE',
)
# Regular expression rules for simple tokens
t_VARIABLE = r'\b[a-z]+\b'
t_WHITESPACE = r'\s+'
t_OR = r'\bOR\b'
t_AND = r'\bAND\b'
t_NOT = r'\bNOT\b'
t_PAR_OPEN = r'\('
t_PAR_CLOSE = r'\)'
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
# Build the lexer
lexer = lex.lex()
lexer.input("a OR (b AND c)")
while True:
token = lexer.token()
if not token:
break
else:
print(token)
这会产生
LexToken(VARIABLE,'a',1,0)
LexToken(WHITESPACE,' ',1,1)
LexToken(OR,'OR',1,2)
LexToken(WHITESPACE,' ',1,4)
LexToken(PAR_OPEN,'(',1,5)
LexToken(VARIABLE,'b',1,6)
LexToken(WHITESPACE,' ',1,7)
LexToken(AND,'AND',1,8)
LexToken(WHITESPACE,' ',1,11)
LexToken(VARIABLE,'c',1,12)
LexToken(PAR_CLOSE,')',1,13)
它甚至可以与嵌套括号一起使用,然后您可以分析较小的部分(例如,从PAR_OPEN
到PAR_CLOSE
等).
It will even work with nested parentheses and you can then analyze smaller parts (e.g. from PAR_OPEN
to PAR_CLOSE
, etc.).
这篇关于简化AND OR布尔表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!