我管理與使用pyparsing模塊的要解決的問題
is True if 'client/.+' and 'user_a' and (not ('limited' and (not ('.+special' or 'godmode'))))
。
import re
import pyparsing
class BoolRegEx(object):
def Match(self, tags=[], query=""):
self.tags = tags
if ' ' not in query:
return self.Search(query)
else:
return pyparsing.operatorPrecedence(
pyparsing.Word(pyparsing.printables, excludeChars="()"), [
(pyparsing.Literal("NOT"), 1, pyparsing.opAssoc.RIGHT, self.Not),
(pyparsing.Literal("OR"), 2, pyparsing.opAssoc.LEFT, self.Or),
(pyparsing.Literal("AND"), 2, pyparsing.opAssoc.LEFT, self.And),
]
).parseString(query, parseAll=True)[0]
def Search(self, a):
try:
regex = re.compile(a.replace("<<", "#~").replace(">>", "~#").replace(">", ")").replace("<", "(").replace("#~", "<").replace("~#", ">"))
for tag in self.tags:
match = regex.match(tag)
if match and len(match.group(0)) == len(tag):
return True
return False
except:
raise
def And(self, t):
for a in t[0][0::2]:
if isinstance(a, basestring):
v = self.Search(a)
else:
v = bool(a)
if not v:
return False
return True
def Or(self, t):
for a in t[0][0::2]:
if isinstance(a, basestring):
v = self.Search(a)
else:
v = bool(a)
if v:
return True
return False
def Not(self, t):
a = t[0][1]
if isinstance(a, basestring):
return not self.Search(a)
else:
return not bool(a)
print BoolRegEx().Match(['client/chat', 'user_a', 'limited', 'extraspecial'], "client/.+ AND user_a AND NOT (limited AND NOT (.+<r|i>special OR godmode))")
# False
print BoolRegEx().Match(['client/chat', 'user_a', 'limited', 'superspecial'], "client/.+ AND user_a AND NOT (limited AND NOT (.+<r|i>special OR godmode))")
# True
我不得不更換與<正則表達式()>,以避免碰撞,但在這一刻這一切似乎是最好的解決辦法。
在這種情況下,我認爲使用整數 - 按位運算符,甚至 - 會是更好的選擇。如果你對這樣做感到滿意,你可以用[此鏈接](http://stackoverflow.com/a/1695250/1079354)中的示例製作一種枚舉對象。 – Makoto 2012-04-23 14:01:37
我不明白這可以用於正則表達式查詢。 'client /.+'本身就是一個查詢。 – 2012-04-23 14:12:23
PyBoolRe非常接近我需要的,但它在複雜的嵌套上失敗。我希望我可以使用mongodb語法及其引擎對進程內列表執行此類查詢,而不需要將列表放入數據庫並通過套接字查詢。 – 2012-04-23 15:05:44