Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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(aa), string)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading