gpt4 book ai didi

python - 如何从字符串中删除所有 IRC 颜色代码

转载 作者:太空宇宙 更新时间:2023-11-04 06:42:34 25 4
gpt4 key购买 nike

注意:我使用的是 Python3

我一直在到处寻找,没有找到完整的东西。在 IRC 上到处询问。我需要一个删除所有 IRC 颜色控制代码的正则表达式。没有一个完整的解决方案。

粗体、斜体、下划线、反转、彩色和纯文本字符数分别为2 29 31 22 3 15。

编辑:

我刚发现一个\x0f 字符也被使用了。

Color 字符 (3) 后面可能最多包含 2 位数字,可能带有逗号,然后最多 2 位数字,或者没有数字只是字符 3。它也可能只是一个逗号,后面有纯文本,其中如果逗号应该留在字符串中。

请帮助我陷入困境。

示例:

'\003' + '12,4' + 'Red and blue' + '\003'+', \031Underline\031' 

12 是蓝色,4 是红色,与字符 3 一起使用。

预期的输出只是“红色和蓝色,下划线”纯文本,没有颜色代码。这样我就可以使用:

line = 'Red and blue, Underline'

line.split(' ')[0] == 'Red'

最佳答案

我整理了一些工作代码,我注意到之前类似代码的帖子中有一个错误导致应用程序崩溃。然后注意到代码可能不起作用,我将其修改为此处的样子。此代码应该按预期工作。它没有经过广泛的测试,但我在编码时确实得到了积极的结果。下面的代码从文本中正确地去除了所有颜色格式的 mIRC 代码;这次。 :/

> def colourstrip(text_with_msl_colour):
> find = text_with_msl_colour.find('\x03')
> while find > -1:
> find_end = find + 1
> done = False
> text_with_msl_colour = text_with_msl_colour[0:find] + text_with_msl_colour[find_end:]
> if len(text_with_msl_colour) - 1 <= find_end:
> done = True
> try:
> assert not done
> done = True
> assert int(text_with_msl_colour[find]) >= 0
> done = False
> text_with_msl_colour = text_with_msl_colour[0:find] + text_with_msl_colour[find_end:]
> if len(text_with_msl_colour) - 1 <= find_end:
> done = True
> assert int(text_with_msl_colour[find]) >= 0
> text_with_msl_colour = text_with_msl_colour[0:find] + text_with_msl_colour[find_end:]
> except:
> pass
> if not done:
> if len(text_with_msl_colour) >= find_end and text_with_msl_colour[find] != ',': done = True
> if (not done) and (len(text_with_msl_colour) > find_end) and (text_with_msl_colour[find] == ','):
> try:
> text_with_msl_colour = text_with_msl_colour[0:find] + text_with_msl_colour[find_end:]
> assert int(text_with_msl_colour[find]) >= 0
> text_with_msl_colour = text_with_msl_colour[0:find] + text_with_msl_colour[find_end:]
> assert int(text_with_msl_colour[find]) >= 0
> text_with_msl_colour = text_with_msl_colour[0:find] + text_with_msl_colour[find_end:]
> done = True
> except:
> done = True
> find = text_with_msl_colour.find('\x03')
> text_with_msl_colour = text_with_msl_colour.replace('\x02', '')
> text_with_msl_colour = text_with_msl_colour.replace('\x1d', '')
> text_with_msl_colour = text_with_msl_colour.replace('\x1f', '')
> text_with_msl_colour = text_with_msl_colour.replace('\x16', '')
> text_with_msl_colour = text_with_msl_colour.replace('\x0f', '')
> return text_with_msl_colour

没有正则表达式可以做到这一点,必须用这里的代码来完成。

关于python - 如何从字符串中删除所有 IRC 颜色代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29247659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com