我正在尝试检查字符串是否包含特定模式中的特定单词,忽略区分大小写并忽略顺序。 我怎么能用正则表达式来做呢?
这是我想要的模式:
'word1-word2'
这些字符串应匹配:
['WORD1-word2','word2-word1']
这些字符串不匹配:
['word3-word2','word1word2']
答案 0 :(得分:0)
也许是这样的:
((?:word|WORD)[1-2]-(?:word|WORD)[1-2])
答案 1 :(得分:0)
re.match("word1-word2|word2-word1", "WORD1-word2", re.IGNORECASE)
答案 2 :(得分:0)
使用锚点:
import re
words = ['WORD1-word2','word2-word1', 'word3-word2','word1word2']
rx = re.compile(r'^(?:word1-word2)|(?:word2-word1)$', re.I)
words_new = list(filter(lambda x: rx.search(x), words))
print(words_new)