我有以下函数返回基于正则表达式的可识别模式。
def labeler(product):
recognized_pattern = re.search(r"(test|estt|stet|tets|ttes|ttse|magne|)\w+(\s)?\w+(.|\s)?\w+", product, re.IGNORECASE)
return recognized_pattern.group()
我将句子传递到函数中,实际上可能在句子中引用了两个或多个产品:例如:“测试 123 和 estt 5000 是很棒的产品。” 目前,该函数仅返回“测试 123 和”
我想增强函数以返回类似这样的数组: name0: 'test 123 and' location0: '<re.Match object; span=(53, 67), match='test 123 and'>' type0: 'product' name1: 'estt 5000 are' location1: '<re.Match object; span=(76, 91), match='estt 5000 are'>' type1: 'product'
如何增强提供上述模式的功能?它如何在一句话中返回多个产品的结构?
附加信息:结果将在稍后作为 REST 响应以 json 形式返回...