【发布时间】:2014-04-23 15:31:55
【问题描述】:
我正在尝试在我的搜索引擎中更多地使用正则表达式。请看:
someStr = "Processor AMD Athlon II X4 651K BOX Black Edition, s. FM1, 3.0GHz, 4MB cache, Quad Core"
# THIS SHOULD MATCH / processors-plural with 0 to 1,
# mega or mb should be the same
# and quad with 0 to 2 of any characters except whitespace
queryListTrue = ["processors", "amd", "4mega", "quaddy"]
# THIS SHOULDN'T MATCH / bad last item length
queryListFalse = ["processors", "amd", "4mb", "quaddie"]
# TO DESCRIBE WHAT I NEED
rulesList = [ r'processor[i.e. 0-1 char]', r'amd',
r'4mega or 4mb', r'quad[from 0 to 2 any char]' ]
if ALL queryListTrue MATCHES someStr THRU rulesList :
print "What a wonderful world!"
任何帮助都会很棒。
【问题讨论】:
-
用正则表达式做黑魔法?
-
我已经通过简单的比较做到了,我认为正则表达式更强大!? ...但是忘记了循环,这很容易,但表达式列表让我很困扰。
标签: python regex search-engine