1 回答
TA贡献1873条经验 获得超9个赞
您发布的代码存在几个问题:
negative_trailers = '|'.join(negative_descriptors) 应该 negative_trailers = '|'.join(negative_trailers )
您还应该像其他列表一样将列表关键字转换为字符串,以便将其传递给正则表达式
在正则表达式中使用 3 次 'r' 是没有用的
经过这些更正后,您的代码应如下所示:
negative_descriptors = ['no', 'unlikely', 'no evidence of']
negative_descriptors = '|'.join(negative_descriptors)
negative_trailers = ['not present', 'not evident']
negative_trailers = '|'.join(negative_trailers)
keywords = ['disc prolapse', 'vertebral osteomyelitis', 'collection']
keywords = '|'.join(keywords)
if re.search(r"("+negative_descriptors+").*("+keywords+")", message): neg_desc_present = True
if re.search(r"("+keywords+").*("+negative_trailers+")", message): neg_desc_present = True
添加回答
举报