我的问题是变成一个看起来像这样的字符串。
"a OR (b AND c)"
into
a OR bc
如果表达式类似于
"a AND (b OR c)"
then gives
ab OR ac
我无法使用REGEX匹配设计正确的循环集。问题的症结在于代码必须完全通用,因为我无法假设字符串模式将持续多长时间,或者OR AND在模式中的确切位置也将不会。
如果我这样输入,它也会解决这种类型的表达式。
答案 0 :(得分:2)
Imo,您将需要在此处使用解析器,例如PLY
。您需要定义所有积木,然后可以构建语法树,使用该语法树可以执行任何您想做的事情。
一个例子可能是:
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
等)。
答案 1 :(得分:0)
通过Pyparsing,可以轻松地为这种符号定义表达式解析器。将您的AND,OR等关键字视为运算符,并将诸如“安全性”之类的术语作为中缀表示法语法中的操作数,您可以使用pyparsing的let wrapper;
let container;
let store = mockStore(initialState);
beforeEach(() => {
wrapper = mount(
<Provider store={store}>
<Component />
</Provider>,
);
container = wrapper.find(Component);
console.log("props:",container.props(),"\nstate:",container.state())
});
语法生成器来定义解析器: / p>
infixNotation
打印:
sample = "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)"
import pyparsing as pp
# enable packrat parsing, since this infix notation gets more complex than usual
pp.ParserElement.enablePackrat()
term = pp.Word(pp.alphas + '*')
SLASH = pp.Suppress('/')
AND = pp.Keyword("AND")
OR = pp.Keyword("OR")
WITHIN = pp.Keyword("WITHIN")
EXCLUDE = pp.Keyword("EXCLUDE")
NEAR_op = pp.Group(pp.Keyword("NEAR") + SLASH + pp.pyparsing_common.integer)
expr = pp.infixNotation(term,
[
(NEAR_op, 2, pp.opAssoc.LEFT),
(WITHIN, 2, pp.opAssoc.LEFT),
(AND, 2, pp.opAssoc.LEFT),
(OR, 2, pp.opAssoc.RIGHT),
(EXCLUDE, 2, pp.opAssoc.LEFT),
])
expr.parseString(sample).pprint()
(免责声明:我是pyparsing的作者。)
GitHub页面:https://github.com/pyparsing/pyparsing
文档:https://pyparsing-docs.readthedocs.io/en/latest/
安装:[[['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']]