You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python实现字符串拆分并将Discord表情替换为[name]格式

Discord入站消息表情格式短化处理

需求说明

  • 核心处理目标:将Discord入站消息中的所有表情统一替换为[表情名称]格式
  • 输入参考:<a:GG:123456789> <:1Copy:12345678><:14:1256678>:eyes:Hello friend!:eyes:
  • 预期输出:[GG] [1Copy][14][eyes]Hello friend![eyes]

初始实现代码

最初编写的处理逻辑仅能部分满足转换需求,针对上述参考输入的实际输出为[GG] [1Copy] [14] [eyes],原始代码如下:

def shorten_emojis(content):
    seperators = ("<a:", "<:")

    output = []

    for chunk in content.split():
        if any(match in chunk for match in seperators):
            parsed_chunk = []

            new_chunk = chunk.replace("<", ";<").replace(">", ">;")

            for emo in new_chunk.split(";"):
                if emo.startswith(seperators):
                    emo = f"<{splits[1]}>" if len(splits := emo.split(":")) == 3 else emo

                parsed_chunk.append(emo)

            chunk = "".join(parsed_chunk)

        output.append(chunk)

    output = " ".join(output)

    for e in re.findall(":.+?:", content):
        output = output.replace(e, f"<{e.replace(':', '')}>")

    return output

实际运行偏差

测试用例1

  • 输入:<a:GG:123456789> <:1Copy:12345678><:14:1256678>:eyes:Hello friend!:eyes:
  • 实际输出:[GG] [1Copy] [14] :eyes:Hello friend!:eyes:
  • 期望输出:[GG] [1Copy][14][eyes]Hello friend![eyes]

测试用例2

  • 输入:<a:cryLaptop:738450655395446814><:1Copy:817543814481707030><:14:817543815401439232> <:thoonk:621279654711656448><:coolbutdepressed:621279653675532290><:KL1Heart:585547199480332318>Nice<:dogwonder:621251869058269185> OK:eyes:
  • 实际输出:[cryLaptop] [1Copy] [14] [thoonk] [coolbutdepressed] [KL1Heart] Nice [dogwonder] OK:eyes:
  • 期望输出:[cryLaptop] [GG] [1Copy] [14] [thoonk] [coolbutdepressed] [KL1Heart] Nice [dogwonder] OK[eyes]

状态说明

目前已针对上述代码的逻辑缺陷完成调整,调整后的输出结果可完全匹配预期要求。


内容的提问来源于stack exchange,提问作者Joshua Nixon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.30 16:36:03