A Regular Expression For IRC Messages

March 02, 2013 — < 1 minute long

Designing an IRC bot and trying to parse incoming messages? Try this:

^ (?:[:]( \ S + ) )?( \ S + )(?: (? ! :)( .+ ?))?(?: [:]( .+ ))? $

It matches to these groups:

prefix, sans colon (e.g. m@m.net) type (PING, PRIVMSG, etc.) destination (#mychannel, mybuddymichael, etc.) message, sans colon.

So using Clojure’s re-matches , "PING :server.net" extracts to

[ "PING :server.net" nil "PING" nil "server.net" ]

And ":m@m.net PRIVMSG #mychannel :Hiya, buddy." would extract to

[ ":m@m.net PRIVMSG #mychannel :Hiya, buddy." "m@m.net" "PRIVMSG" "#mychannel" "Hiya, buddy." ]

This regex is a fine-tuning of Matthew DeSantis’s IRC regex. Much thanks to him.

This and other works on this site are licensed under a Creative Commons Attribution-Share Alike license.