简体   繁体   English

即使有重复,也要在一个字符之后获取所有内容。 蟒蛇

[英]Get everything after a character, even when there are repeats. Python

I'm writing an IRC bot, and am parsing the message using a whole bunch of splits. 我正在编写一个IRC机器人,并使用大量拆分来解析消息。 The format for an IRC 'message' is: IRC“消息”的格式为:

:username!765a4aa2@gateway/web/freenode/ip.***.***.***.*** PRIVMSG #Channel :Message body

In particular is the one that get's the text of the message: 特别是得到消息文本的消息:

message = data.split(':')[2]

This get's everything after the last ':' But when the user sends a link over IRC, then the message will look like: 最后一个':'之后的所有内容都可以获取,但是当用户通过IRC发送链接时,该消息将如下所示:

:username!765a4aa2@gateway/web/freenode/ip.***.***.***.*** PRIVMSG #Channel :http://web address.com/

and the code will only get the 'http' section of the message, as the rest is now in the third section of the split. 该代码将仅获得消息的“ http”部分,因为其余部分现在位于拆分的第三部分。

So how can I parse a message containing a link? 那么,如何解析包含链接的消息?

One way is to get the lenght of the first section, and remove it: 一种方法是获取第一部分的长度,然后将其删除:

message = data[len(data.split(':')[1])+1:]

But I can't help feeling that there must be a better way. 但是我不禁感到必须有更好的方法。 Is there one? 有一个吗?

str.split()接受可选参数maxsplit

message = data.split(':', 2)

Set the amount of : you want to split: 设置的数量:要分割:

data.split(':', 2)

And you'll have: 您将拥有:

['',
'username!765a4aa2@gateway/web/freenode/ip.***.***.***.*** PRIVMSG #Channel ',
'http://web address.com/']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM