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

Replacing regexp patterns with different values

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

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

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? Thanks in advance

>Solution :

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

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