re.sub将模板转换为函数

时间:2019-02-07 04:04:31

标签: python regex

我正在使用re.sub将ARB样式的名称替换为字符串中的GLSL样式的名称。现在,我想将所有转换后的匹配项另外存储到一组字符串中。我可以在仍然使用re.sub的“模板”语法的同时这样做吗?

代码如下:

# set of replacement rules
expl_table = [
    (r'program.env\[(\d+)\]'  , r'program_env_\1'),
    (r'program.local\[(\d+)\]', r'program_local_\1'),
]
for props in expl_table:
    (re_from, re_to) = props
    # arg = re.sub(re_from, re_to, arg)       # simple and good
    def replace_func(m):
        result = ??repl_template??(m, re_to)  # where can I find it?
        declarations.append(result)           # want to save all replacements
        return result
    arg = re.sub(re_from, replace_func, arg)

我在source code中发现了类似_subx的地方,但似乎已关闭。 看来我必须自己实现它,听起来很愚蠢。

1 个答案:

答案 0 :(得分:1)

您可以在使用re.finditer()遍历字符串时更改字符串:

# set of replacement rules
expl_table = [
    (r'program.env\[(\d+)\]'  , r'program_env_dsdsds\1'),
    (r'program.local\[(\d+)\]', r'program_local_\1'),
]

declarations = []
for props in expl_table:
    (re_from, re_to) = props

    offset = 0
    for m in re.finditer(re_from, string):
        sub = m.expand(re_to)

        string = string[:m.start()+offset] + sub + string[m.end()+offset:]
        offset = max(map(len, [sub, m.group(0)])) - min(map(len, [sub, m.group(0)]))
        declarations.append(sub)

print(string)

或者,您可以在同一范围内“升级” lambda函数。通常,不允许在lambda函数中使用多个语句,但是列表理解在某种程度上绕过了该约束:

for props in expl_table:
    (re_from, re_to) = props
    string = re.sub(re_from,
                lambda m: [
                           (result, declarations.append(result)) 
                           for result in [m.expand(re_to)]
                          ][0][0],
                 string)

print(string)
print(declarations)