有什么办法可以删除空文本?

时间:2020-03-25 00:17:21

标签: python

所以我有这行

invite = re.search("discord.gg/(.*)", ctx.content).group(1)

此行在不和谐聊天中搜索邀请,邀请看起来像此discord.gg/82181,但有时有人像discord.gg/82 181那样放下它,因此您在其中看到一个空白。 *)是代码(82181),如果检测到空格,是否可以自动删除该空格?谢谢。

1 个答案:

答案 0 :(得分:2)

使用replace()函数。

invite = re.search("discord.gg/(.*)", ctx.content).group(1).replace(' ', '')

如果邮件中的数字后面可能包含其他内容,则可以使.*更加具体,以免被捕获。

invite = re.search("discord.gg/([\d ]*)", ctx.content).group(1).replace(' ', '')