Home > Enterprise >  Replacing regexp patterns with different values
Replacing regexp patterns with different values

Time:08-16

I have string like this:

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

and i need replace {panel:title=Реализация|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? T

CodePudding user response:

Try:

import re

text = "bla bla bla\n{panel:title=Realisation|borderStyle=dashed|borderColor=#cccccc|titleBGColor=#a39d9d}\nbla bla bla\n{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

  • Related