pythonpython-re

How to substitute each regex pattern with a corresponding item from a list


I have a string that I want to do regex substitutions on:

string = 'ptn, ptn; ptn + ptn'

And a list of strings:

array = ['ptn_sub1', 'ptn_sub2', '2', '2']

I want to replace each appearance of the regex pattern 'ptn' with a corresponding item from array.

Desired result: 'ptn_sub1, ptn_sub2; 2 + 2'

I tried using re.finditer to iterate through the matches and substitute each time, but this caused the new string to get mangled since the length of the string changes with each iteration.

import re

matches = re.finditer(r'ptn', string)
new_string = string
for i, match in enumerate(matches):
    span = match.span()
    new_string = new_string[:span[0]] + array[i] + new_string[span[1]:]

Mangled output: 'ptn_sptn_s2, ptn2tn + ptn'

How can I do these substitutions correctly?


Solution

  • As stated, one just have to use the "callback" form of re.sub - but it is actually simple enough:

    import re
    string = 'ptn, ptn; ptn + ptn'
    array = ['ptn_sub1', 'ptn_sub2', '2', '2']
    
    array_iter = iter(array)
    result = re.sub("ptn", lambda m: next(array_iter), string)