我想要一个正则表达式python代码:
我对正则表达式并不擅长。
答案 0 :(得分:6)
为什么要这么麻烦?
>>> 'FOO'.lower() in set(('foo', 'bar', 'baz'))
True
>>> 'Quux'.lower() in set(('foo', 'bar', 'baz'))
False
答案 1 :(得分:0)
经过多次谷歌搜索,并且试错了,我创建了一个解决方案,用于将多个单词与字符输入分开。
import re
keywords = ('cars', 'jewelry', 'gas')
pattern = re.compile('[a-z]+', re.IGNORECASE)
txt = 'GAS, CaRs, Jewelrys'
keywords_found = pattern.findall(txt.lower())
n = 0
for i in keywords_found:
if i in keywords:
print keywords_found[n]
n = n + 1
答案 2 :(得分:0)
使用set
而不是循环,您的自我回答会更好。
将i
用于文本变量而将n
用于索引则非常违反直觉。 keywords_found
是用词不当。
试试这个:
>>> import re
>>> keywords = set(('cars', 'jewelry', 'gas'))
>>> pattern = re.compile('[a-z]+', re.IGNORECASE)
>>> txt = 'GAS, CaRs, Jewelrys'
>>> text_words = set(pattern.findall(txt.lower()))
>>> print "keywords:", keywords
keywords: set(['cars', 'gas', 'jewelry'])
>>> print "text_words:", text_words
text_words: set(['cars', 'gas', 'jewelrys'])
>>> print "text words in keywords:", text_words & keywords
text words in keywords: set(['cars', 'gas'])
>>> print "text words NOT in keywords:", text_words - (text_words & keywords)
text words NOT in keywords: set(['jewelrys'])
>>> print "keywords NOT in text words:", keywords - (text_words & keywords)
keywords NOT in text words: set(['jewelry'])