如何连接正则表达式模式?

时间:2018-12-17 18:32:41

标签: python regex dictionary replace

我正在尝试使用字典替换html文件中的文本。

这有效,但不幸的是,如果该文本被html标记包围或在其旁边带有逗号,则该文本不匹配:

for key in dictionary:
    print(key)
    if key in answer_string:
        pattern = re.compile(key, re.IGNORECASE)
        answer_string = re.sub(r"[^ ]*"+pattern+r"[^ ]*", "<a href=\"" + dictionary.get(key) + "\">" + key + "</a>", answer_string)

这是我尝试的方法,但是不起作用。我收到错误消息:TypeError: cannot concatenate 'str' and '_sre.SRE_Pattern' objects

for key in dictionary:
    print(key)
    if key in answer_string:
        pattern = re.compile(key, re.IGNORECASE)
        answer_string = re.sub(r"[^ ]*"+pattern+r"[^ ]*", "<a href=\"" + dictionary.get(key) + "\">" + key + "</a>", answer_string)

1 个答案:

答案 0 :(得分:2)

MATCH p =(a:Crime{type:'Burglary'})-[:OCCURRED_AT*2]-(b:Crime{type:'Bicycle theft'}) WHERE a.date > b.date return p LIMIT 100; 返回正则表达式编译对象,而不是字符串。您只需要在连接字符串之前对其进行编译:

re.compile