Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question