M
M
mmserebryakov2021-12-20 15:09:20
Python
mmserebryakov, 2021-12-20 15:09:20

Removing {color:#1f497d} characters from text Python?

Please help me write a regular expression that would catch the name of the colors in the text in html. Example:
{color:#1f497d}Some task test{color}{color:#1f497d}MAC{color}{color:#1f497d}{color}{color:#1f497d}server
text.{color} etc. d.
I have now written through the cycle of all the names of the colors that I caught, but they appear, I want to rewrite the solution.

def text_clear(text_string):
    bad_chars = ['* {color:#000000}', '{color}', '{color:#172B4D}', '{color:#000000}',
                 '{color:#C82613}', '{color:#C82613}', '{color:#1f497d}', '{color:black}', '*',
                 '{color:#1f4e79}', '{color:#222222}', '{color:#1F497D}', '{color:#333333}',
                 '•', '{color:#404040}', '{color:#000012}']

    try:
        for i in bad_chars:
            text_string = text_string.replace(i, '').strip()
    except AttributeError:
        text_string = None
    return text_string

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2021-12-20
@mmserebryakov

re.sub(r'\{color.*?\}', '', text)

V
Vlad Grigoriev, 2021-12-20
@Vaindante

import re

text = '''
{color:#1f497d}Какой-то тест задачи{color}{color:#1f497d}MAC{color}{color:#1f497d}{color}{color:#1f497d}сервер
текст.{color}
'''

rm_color = re.compile(r'\{color:#[A-Fa-f0-9]{6}}|\{color}')
print(rm_color.sub(' ', text))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question