我有一个工作例程来确定新闻项目所属的类别。在Python中为标题,类别,子类别和搜索词指定值为RegExp时,该例程有效。
但是当从PostgreSQL中检索这些值作为字符串时,我不会得到任何错误,或者来自同一例程的结果。
我检查了数据类型,两者都是Python字符串。
可以采取哪些措施来解决这个问题?
# set the text to be analyzed
title = "next week there will be a presentation. The location will be aat"
# these could be the categories
category = "presentation"
subcategory = "scientific"
# these are the regular expressions
main_category_search_words = r'\bpresentation\b'
sub_category_search_words= r'\basm microbe\b | \basco\b | \baat\b'
category_final = ''
subcategory_final = ''
# identify main category
r = re.compile(main_category_search_words, flags=re.I | re.X)
result = r.findall(title)
if len(result) == 1:
category_final = category
# identify sub category
r2 = re.compile(sub_category_search_words, flags=re.I | re.X)
result2 = r2.findall(title)
if len(result2) > 0:
subcategory_final = subcategory
print("analysis result:", category_final, subcategory_final)
答案 0 :(得分:1)
我非常确定您从PostgreSQL获得的内容不是raw string literal,因此您的RegEx无效。您必须在DB中明确地转义模式中的反斜杠。
print(r"\basm\b")
print("\basm\b")
print("\\basm\\b")
# output
\basm\b
as # yes, including the line break above here
\basm\b