Skip to main content
 首页 » 编程设计

python之Python 中已编译的正则表达式列表

2025年05月04日23zhengyun_ustc

我有很多文本清理需要的替换模式。出于性能原因,我之前从数据库加载数据并编译正则表达式。 不幸的是,在我的方法中,只有变量“text”的最后一个赋值似乎是有效的,而其他的似乎被覆盖了:

# -*- coding: utf-8 -*- 
import cx_Oracle 
import re 
 
connection = cx_Oracle.connect("SCHEMA", "passWORD", "TNS") 
cursor = connection.cursor() 
cursor.execute("""select column_1, column_2 
from table""") 
 
# Variables for matching 
REPLACE_1 = re.compile(r'(sample_pattern_1)') 
REPLACE_2 = re.compile(r'(sample_pattern_2)') 
# .. 
REPLACE_99 = re.compile(r'(sample_pattern_99)') 
REPLACE_100 = re.compile(r'(sample_pattern_100)') 
 
def extract_from_db(): 
    text = '' 
    for row in cursor: 
        # sidenote: each substitution text has the the name as the corresponding variable name, but as a string of course 
        text = REPLACE_1.sub(r'REPLACE_1',str(row[0])) 
        text = REPLACE_2.sub(r'REPLACE_2',str(row[0])) 
        # .. 
        text = REPLACE_99.sub(r'REPLACE_99',str(row[0])) 
        text = REPLACE_100.sub(r'REPLACE_199',str(row[0])) 
        print text 
 
extract_from_db() 

有谁知道如何以有效、优雅的方式解决这个问题?还是我必须通过巨大的 if/elif 控制结构来解决这个问题?

请您参考如下方法:

您不断用 str(row[0]) 上的替换替换最后一个结果。使用 text 来累积替换:

text = REPLACE_1.sub(r'REPLACE_1', str(row[0])) 
text = REPLACE_1.sub(r'REPLACE_1', text) 
# .. 
text = REPLACE_99.sub(r'REPLACE_99', text) 
text = REPLACE_100.sub(r'REPLACE_199', text) 

您最好使用实际的列表:

REPLACEMENTS = [ 
    (re.compile(r'(sample_pattern_1)'), r'REPLACE_1'), 
    (re.compile(r'(sample_pattern_2)'), r'REPLACE_2'), 
    # .. 
    (re.compile(r'(sample_pattern_99)'), r'REPLACE_99'), 
    (re.compile(r'(sample_pattern_100)'), r'REPLACE_100'), 
] 

并在循环中使用它们:

text = str(row[0]) 
for pattern, replacement in REPLACEMENTS: 
    text = pattern.sub(replacement, text) 

或使用 functools.partial()进一步简化循环:

from functools import partial 
 
REPLACEMENTS = [ 
    partial(re.compile(r'(sample_pattern_1)').sub, r'REPLACE_1'), 
    partial(re.compile(r'(sample_pattern_2)').sub, r'REPLACE_2'), 
    # .. 
    partial(re.compile(r'(sample_pattern_99)').sub, r'REPLACE_99'), 
    partial(re.compile(r'(sample_pattern_100)').sub, r'REPLACE_100'), 
] 

和循环:

text = str(row[0]) 
for replacement in REPLACEMENTS: 
    text = replacement(text) 

或者使用上面的包装在 partial() 对象中的模式列表,以及 reduce() :

text = reduce(lambda txt, repl: repl(txt), REPLACEMENTS, str(row[0])