test_reg.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334
  1. import re
  2. def highlight_long_common_substrings(str_a, str_b):
  3. """
  4. 提取 A 和 B 中长度大于1的共同字符,并将 A 中的这些字符用 <strong> 标记
  5. """
  6. # 1. 从字符串 B 中提取所有长度大于1的连续字符片段(过滤掉正则符号)
  7. # \w+ 会匹配字母、数字和下划线(如果你想匹配中文、字母和数字,可以保留\w;如果只想匹配纯中文,可以改成 [\u4e00-\u9fff]+)
  8. b_substrings = re.findall(r'[\w\u4e00-\u9fff]{2,}', str_b)
  9. # 2. 去重,并按照长度从长到短排序
  10. # 排序非常重要!这能确保先匹配长词(如“海马”),避免短词(如“海马”被拆成“海”和“马”)干扰
  11. unique_substrings = sorted(set(b_substrings), key=len, reverse=True)
  12. highlighted_a = str_a
  13. # 3. 遍历这些长字符串,如果在 A 中出现,就进行高亮替换
  14. for substring in unique_substrings:
  15. if substring in highlighted_a:
  16. # 使用 re.sub 进行替换,re.escape 用于防止字符串中包含特殊正则符号报错
  17. highlighted_a = re.sub(f'({re.escape(substring)})', r'<strong>\1</strong>', highlighted_a)
  18. return highlighted_a
  19. def generate_html(phrase, matched_rule, tag_name):
  20. highlighted_phrase = highlight_long_common_substrings(phrase, matched_rule)
  21. html_output = f"映射文本【{highlighted_phrase}】与映射规则【{matched_rule}】匹配,映射为标签【{tag_name}】"
  22. return html_output
  23. phrase = "投向:环境保护专用设备制造; 用途:养海马"
  24. matched_rule = "环境保护专用设备制造|环境监测专用仪器仪表制造"
  25. tag_name = "环保专用设备仪器制造业"
  26. print(generate_html(phrase, matched_rule, tag_name))