Replacing regexp patterns with different values

Question:

I have string like this:

text = "bla bla blan {panel:title=Realisation|borderStyle=dashed|borderColor=#cccccc|titleBGColor=#a39d9d}n bla bla blan {panel:title=Definition of Done|borderStyle=dashed|borderColor=#cccccc|titleBGColor=#a39d9d} bla bla

and i need replace {panel:title=Realisation|borderStyle=dashed|borderColor=#cccccc|titleBGColor=#a39d9d} to Realisation and {panel:title=Definition of Done|borderStyle=dashed|borderColor=#cccccc|titleBGColor=#a39d9d} to Definition of Done

I tried to do it like this

def del_tags(text):
    if text:
        CLEANR = re.compile('{panel.*?}')
        try:
            TITLES = re.search('title=(.+?)|', text).group(1)
        except AttributeError:
            TITLES = ""
        cleantext = re.sub(CLEANR, TITLES, text)
        return cleantext
    else: 
        return text

But they replace everything with Realisation.

How can I solve my problem?

Asked By: De1f

||

Answers:

Try:

import re

text = "bla bla blan{panel:title=Realisation|borderStyle=dashed|borderColor=#cccccc|titleBGColor=#a39d9d}nbla bla blan{panel:title=Definition of Done|borderStyle=dashed|borderColor=#cccccc|titleBGColor=#a39d9d} bla bla"

text = re.sub(r"{panel:title=([^|}]+).*?}", r"1", text)
print(text)

Prints:

bla bla bla
Realisation
bla bla bla
Definition of Done bla bla

{panel:title=([^|}]+).*?} will match title after the panel:title= and substitute the whole {...}regex101

Answered By: Andrej Kesely
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.