前言

  • 技术栈
Python    3.11
regex     2023.5.5

案例

  • 测试代码
# encoding: utf-8
# author: qbit
# date: 2024-04-24
# summary: 利用正则转换参数化表达式

import regex

line1 = 'owner=x_111 AND doc_type=%x%_222 OR author=x_333 OR organ=x_444 AND (NOT pub_year=x_555)'
dic = {
    '111': 'aaa',
    '222': 'bbb',
    '333': 'ccc',
    '444': 'ddd',
    '555': 'eee',
}

pattern = regex.compile(r'''(?<field>[a-z_\d+]+)=(?P<mode>[a-z%\d]+)_(?<key>[a-z\d]+)''')
def rulerepl(matobj: regex.Match):
    field = matobj['field']
    mode = matobj['mode']
    key = matobj['key']
    match mode:
        case r'x':     # 全等精确匹配
            rule=f"{field}:({dic[key]})"
        case r'%x%':     # 前后模糊(*abc*)
            rule=f"{field}:(*{dic[key]}*)"
        case _:    
            raise Exception(f"error mode {mode}")
    return rule

line2 = regex.sub(pattern, rulerepl, line1)  
print(f"转换前: {line1}")
print(f"转换后: {line2}")
  • 测试输出
转换前: owner=x_111 AND doc_type=%x%_222 OR author=x_333 OR organ=x_444 AND (NOT pub_year=x_555)
转换后: owner:(aaa) AND doc_type:(*bbb*) OR author:(ccc) OR organ:(ddd) AND (NOT pub_year:(eee))

相关资料

文章

本文出自 qbit snap

qbit
268 声望279 粉丝