我正在用 Java 创建一个 irc 客户端。它工作正常,但来自服务器的消息有点“困惑”
例如 :User1!webirc@1.9.com PRIVMSG #channel :test
。所以我想知道如何将 irc 消息解析为人类可读的?这是我为 irc 消息找到的 ^(:(\\S+) )?(\\S+)( (?!:)(.+?))?( :(.+))?$
的正则表达式。
最佳答案
IRC 协议(protocol)记录如下:https://www.rfc-editor.org/rfc/rfc2812
2.3.1 Message format in Augmented BNF
The protocol messages must be extracted from the contiguous stream of octets. The current solution is to designate two characters, CR and LF, as message separators. Empty messages are silently ignored, which permits use of the sequence CR-LF between messages without extra problems.
The extracted message is parsed into the components , and list of parameters ().
The Augmented BNF representation for this is:
message = [ ":" prefix SPACE ] command [ params ] crlf
prefix = servername / ( nickname [ [ "!" user ] "@" host ] )
command = 1*letter / 3digit
params = *14( SPACE middle ) [ SPACE ":" trailing ]
=/ 14( SPACE middle ) [ SPACE [ ":" ] trailing ]
nospcrlfcl = %x01-09 / %x0B-0C / %x0E-1F / %x21-39 / %x3B-FF
; any octet except NUL, CR, LF, " " and ":"
middle = nospcrlfcl *( ":" / nospcrlfcl )
trailing = *( ":" / " " / nospcrlfcl )
SPACE = %x20 ; space character
crlf = %x0D %x0A ; "carriage return" "linefeed"
关于Java - 如何将 irc 消息解析为人类可读的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21016372/