How can I add a string inside a string?

Question:

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"

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
Asked By: Pabloflopygame

||

Answers:

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.

Answered By: Michael Delgado
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.