简体   繁体   English

使用RE查找单词的匹配项

[英]Use RE to find matching of words

I'm trying to check if string contains specific words in a specific pattern, ignore case sensitive and ignore the order. 我正在尝试检查字符串是否在特定模式中包含特定单词,忽略大小写并忽略顺序。 How can I do it in regular expression? 我该如何做正则表达式?

This is the pattern I want: 这是我想要的模式:

'word1-word2'

These strings should match: 这些字符串应匹配:

['WORD1-word2','word2-word1']

These strings shouldn't match: 这些字符串不应该匹配:

['word3-word2','word1word2']

也许像这样:

((?:word|WORD)[1-2]-(?:word|WORD)[1-2])
re.match("word1-word2|word2-word1", "WORD1-word2", re.IGNORECASE)

Use anchors: 使用锚点:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM