Python 正则表达式字典[谷歌类型搜索并匹配正则表达式]
编辑:下面代码的主要问题之一是将正则表达式对象存储在字典中,以及如何访问它们以查看它们是否可以匹配其他字符串。但我还是会留下我之前的问题,因为我认为可能有一个简单的方法来完成所有这些 我想在python中找到一个方法,它知道如何返回两个字符串是否引用同一事物的布尔值。我知道这在编程中是很困难的,如果不是完全荒谬的话,但是我正在研究如何使用一个涉及相同内容的可选字符串字典来处理这个问题 这里有一些例子,因为我知道如果没有它们,这是没有意义的 如果我给出字符串:Python 正则表达式字典[谷歌类型搜索并匹配正则表达式],python,regex,search,dictionary,full-text-search,Python,Regex,Search,Dictionary,Full Text Search,编辑:下面代码的主要问题之一是将正则表达式对象存储在字典中,以及如何访问它们以查看它们是否可以匹配其他字符串。但我还是会留下我之前的问题,因为我认为可能有一个简单的方法来完成所有这些 我想在python中找到一个方法,它知道如何返回两个字符串是否引用同一事物的布尔值。我知道这在编程中是很困难的,如果不是完全荒谬的话,但是我正在研究如何使用一个涉及相同内容的可选字符串字典来处理这个问题 这里有一些例子,因为我知道如果没有它们,这是没有意义的 如果我给出字符串: 'breakingBad.Seaso
'breakingBad.Season+01集..02'
然后我希望它与字符串匹配:
“破坏S01E02”
或者“三桶水+一桶水”
可以匹配“三桶水”
我知道这几乎是不可能做到的,因为'3'
和'water'
等是同义词,但如果需要,我愿意将它们作为相关正则表达式同义词的字典提供给函数
我有一种感觉,在python中有一种更简单的方法来实现这一点,就像往常一样,但到目前为止,我已经有了:
import re
def check_if_match(given_string, string_to_match, alternative_dictionary):
print 'matching: ', given_string, ' against: ', string_to_match
# split the string into it's parts with pretty much any special character
list_of_given_strings = re.split(' |\+|\.|;|,|\*|\n', given_string)
print 'List of words retrieved from given string: '
print list_of_given_strings
check = False
counter = 0
for i in range(len(list_of_given_strings)):
m = re.search(list_of_given_strings[i], string_to_match, re.IGNORECASE)
m_alt = None
try:
m_alt = re.search(alternative_dictionary[list_of_given_strings[i]], string_to_match, re.IGNORECASE)
except KeyError:
pass
if m or m_alt:
if counter == len(list_of_given_strings)-1: check = True
else: counter += 1
print list_of_given_strings[i], ' found to match'
else:
print list_of_given_strings[i], ' did not match'
break
return check
string1 = 'breaking Bad.Season+01 Episode..02'
other_string_to_check = 'Breaking.Bad.S01+E01'
# make a dictionary of synonyms - here we should be saying that "S01" is equivalent to "Season 01"
alternative_dict = {re.compile(r'S[0-9]',flags=re.IGNORECASE):re.compile(r'Season [0-9]',flags=re.IGNORECASE),\
re.compile(r'E[0-9]',flags=re.IGNORECASE):re.compile(r'Episode [0-9]',flags=re.IGNORECASE)}
print check_if_match(string1, other_string_to_check, alternative_dict)
print
# another try
string2 = 'three.BuCkets+of H2O'
other_string_to_check2 = '3 buckets of water'
alternative_dict2 = {'H2O':'water', 'three':'3'}
print check_if_match(string2, other_string_to_check2, alternative_dict2)
这将返回:
matching: breaking Bad.Season+01 Episode..02 against: Breaking.Bad.S01+E01
List of words retrieved from given string:
['breaking', 'Bad', 'Season', '01', 'Episode', '', '02']
breaking found to match
Bad found to match
Season did not match
False
matching: three.BuCkets+of H2O against: 3 buckets of water
List of words retrieved from given string:
['three', 'BuCkets', 'of', 'H2O']
three found to match
BuCkets found to match
of found to match
H2O found to match
True
我意识到这可能意味着字典键和值有问题,但我觉得我离一个简单的pythonic解决方案越来越远了,这个解决方案可能已经创建好了
有人有什么想法吗?我正在修补它,发现了一些有趣的东西:
- 这可能与你将最初的单词分解成列表的方式有关
matching: breaking Bad.Season 1.Episode.1 against: Breaking.Bad.S1+E1 List of words retrieved from given string: ['breaking', 'Bad', 'Season', '1', 'Episode', '1']
- 我想您希望它是
,而不是将,“第1季”,…
和“第1季”
作为列表中的单独条目1
- 您指定了
,但这与两位数不匹配S[0-9]
- 关于你的常规表达式存储在字典中的说法是对的;映射仅适用于一个方向。我通过将
映射到r'seasure[0-9]
而不是反之亦然来处理代码(不幸的是,我不记得它是什么),它能够匹配r'S[0-9]'
seasure
- 不是映射,而是为每种字符串类型(例如标题、季节、插曲)创建一个等价类,并为此编写一些匹配器代码
- 分离解析和比较步骤。将每个字符串分别解析为通用格式或对象,然后进行比较
- 您可能需要实现某种状态机来知道您正在处理一个季节,并期望在它之后立即看到一个特定格式的数字
- 您可能希望使用第三方工具;我听说了一些好消息