注意:我使用的是Python3
我到处找都找不到完整的东西。到处在IRC上询问。我需要一个正则表达式,删除所有的IRC颜色控制代码。任何地方都没有一个完整的解决方案。
粗体、斜体、下划线、反转、彩色和纯文本数字分别为2 29 31 22 3 15。
编辑:
我刚发现也在使用\x0f字符。
颜色字符( 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'
发布于 2015-04-15 16:28:10
我知道我需要一个正则表达式解决方案,但我最终还是开始编写一个有效的非正则表达式解决方案。
我更新了代码以与颜色代码更兼容;允许无限颜色代码,因为irc客户端从第一个颜色(0表示白色)开始环绕颜色,然后是颜色列表的末尾,依此类推。因此,现在colourstrip()将处理颜色数字,而不是要求颜色数字最多为2位的旧代码,这是没有意义的。
def colourstrip(data):
find = data.find('\x03')
while find > -1:
done = False
data = data[0:find] + data[find+1:]
if len(data) <= find+1:
done = True
try:
assert not done
assert int(data[find])
while True:
assert int(data[find])
data = data[0:find] + data[find+1:]
except:
if not done:
if data[find] != ',': done = True
if (not done) and (len(data) > find+1) and (data[find] == ','):
try:
assert not done
assert int(data[find+1])
data = data[0:find] + data[find+1:]
data = data[0:find] + data[find+1:]
except:
done = True
try:
assert not done
while True:
assert int(data[find])
data = data[0:find] + data[find+1:]
except: pass
find = data.find('\x03')
data = data.replace('\x02','')
data = data.replace('\x1d','')
data = data.replace('\x1f','')
data = data.replace('\x16','')
data = data.replace('\x0f','')
return data
datastring = '\x03123434,27384This is coolour \x032689,34344This is too\x03'
print(colourstrip(datastring))
谢谢大家的帮助。
发布于 2021-05-13 11:22:19
我整理了一些正常工作的代码,我注意到前一篇类似代码中的一个bug导致了应用程序崩溃。然后注意到代码可能不起作用,我将其修改为此处的样子。这段代码应该可以按预期工作。它没有经过广泛的测试,但我在编码时得到了肯定的结果。下面的代码正确地从文本中剥离所有颜色格式的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
没有正则表达式可以做到这一点,它必须用这里的代码来完成。
发布于 2015-03-25 16:17:23
[\x02\x0F\x16\x1D\x1F]|\x03(\d{,2}(,\d{,2})?)?
这将匹配您提到的所有IRC格式化代码。在颜色代码的情况下,它甚至可以捕获格式错误的代码,如\x03,11
、\x034,
和\x03,
。我意识到这可能是理想的,也可能不是理想的,这取决于你希望如何处理像这样的错误代码,但你可以很容易地调整它来做你想做的事情。如果需要,你可以解释你希望如何处理这些问题,我可以更新答案以反映这一点。
至于该怎么做,一种解决方案是:
pattern = r'[\x02\x0F\x16\x1D\x1F]|\x03(\d{,2}(,\d{,2})?)?';
text = '\x0312,4Text\x03';
stripped = re.sub(pattern, '', text);
另请参阅Python文档的Section 6.2。
https://stackoverflow.com/questions/29247659
复制相似问题