繁体   English   中英

正则表达式忽略大小写匹配,但保留特定大小写的结果

[英]Regex ignore case for match , but keep a specific case for results

我正在寻找解决此问题的有效方法

假设我们要在忽略大小写的情况下查找字符串中的单词列表,但与其存储匹配的字符串,我们不希望使用与原始列表具有相同大小写的字符串。

例如 :

words_to_match = ['heLLo', 'jumP', 'TEST', 'RESEARCH stuff']
text = 'hello this is jUmp test jump and research stuff'
# Result should be {'TEST', 'heLLo', 'jumP', 'RESEARCH stuff'}

这是我目前的方法:

words_to_match = ['heLLo', 'jumP', 'TEST', 'RESEARCH stuff']

我将其转换为以下正则表达式:

regex = re.compile(r'\bheLLo\b|\bjumP\b|\bTEST\b|\bRESEARCH stuff\b', re.IGNORECASE)

然后

word_founds = re.findall(regex,'hello this is jUmp test jump and research stuff')
normalization_dict = {w.lower():w for w in words_to_match}
# normalization dict : {'hello': 'heLLo', 'jump': 'jumP', 'test': 'TEST', 'research stuff': 'RESEARCH stuff'}
final_list = [normalization_dict[w.lower()] for w in word_founds]
# final_list : ['heLLo', 'jumP', 'TEST', 'jumP', 'RESEARCH stuff']
final_result = set(final_list)
# final_result : {'TEST', 'heLLo', 'jumP', 'RESEARCH stuff'}

这是我的预期结果,我只想知道是否有更快/更优雅的方法来解决此问题。

如果您仍然可以使用正则表达式,则可以单行完成。

results = set(word for word in re.findall(r"[\w']+", text) if word.lower() in [w.lower() for w in words_to_match])

它仅用于根据单词边界拆分text变量。

编辑:

您还可以使用:

import string
results = set(word for word in "".join(c if c not in string.punctuation else " " for c in text).split() 
              if word.lower() in [w.lower() for w in words_to_match])

如果要避免导入re ,则必须使用string

编辑2 :(希望在正确阅读问题之后)

results = set(word for word in words_to_match if word.lower() in text.lower())

这也适用于多字搜索。

编辑3:

results = set(word for word in words_to_match if re.search(r"\b" + word.lower() + r"\b", text.lower()))

尝试这个:

words_to_match = ['heLLo', 'jumP', 'TEST'] 
text = 'hello this is jUmp test jump'
result = set()
for str in words_to_match:
    if str.lower() in text.lower():
        result.add(str)

暂无
暂无

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

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