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 can I add a string inside a string?

The problem is simple, I’m given a random string and a random pattern and I’m told to get all the posible combinations of that pattern that occur in the string and mark then with [target] and [endtarget] at the beggining and end.

For example:

given the following text: "XuyZB8we4"

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

and the following pattern: "XYZAB"

The expected output would be: "[target]X[endtarget]uy[target]ZB[endtarget]8we4".

I already got the part that identifies all the words, but I can’t find a way of placing the [target] and [endtarget] strings after and before the pattern (called in the code match).

import re


def tagger(text, search):
    place_s = "[target]"
    place_f = "[endtarget]"
    pattern = re.compile(rf"[{search}]+")
    matches = pattern.finditer(text)

    for match in matches:
        print(match)

    return test_string


test_string = "alsikjuyZB8we4 aBBe8XAZ piarBq8 Bq84Z "
pattern = "XYZAB"

print(tagger(test_string, pattern))

I also tried the for with the sub method, but I couldn’t get it to work.

    for match in matches:
        re.sub(match.group(0), place_s + match.group(0) + place_f, text)

    return text

>Solution :

re.sub allows you to pass backreferences to matched groups within your pattern. so you do need to enclose your pattern in parentheses, or create a named group, and then it will replace all matches in the entire string at once with your desired replacements:

In [10]: re.sub(r'([XYZAB]+)', r'[target]\1[endtarget]', test_string)
Out[10]: 'alsikjuy[target]ZB[endtarget]8we4 a[target]BB[endtarget]e8[target]XAZ[endtarget] piar[target]B[endtarget]q8 [target]B[endtarget]q84[target]Z[endtarget] '

With this approach, re.finditer is not not needed at all.

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