A little while back I was trying to make a small IRC bot but I eventually lost my interest in it. While writing the bot I had to write a regex to match the raw IRC message pattern. A friend (thanks Jobe) on IRC came up with the following regex:

^(?:[:@]([^\\s]+) )?([^\\s]+)(?: ((?:[^:\\s][^\\s]* ?)*))?(?: ?:(.*))?$

It will match 4 groups (source, command, target and the parameters). A small example in JAVA:

Pattern pattern = Pattern.compile("^(?:[:@]([^\\s]+) )?([^\\s]+)(?: ((?:[^:\\s][^\\s]* ?)*))?(?: ?:(.*))?$");
Matcher matcher = pattern.matcher(line.subSequence(0, line.length()));
 
if (matcher.matches()) {
	//i.e irc.mibbit.net
	source = matcher.group(1);
	//i.e 433/NOTICE
	cmd = matcher.group(2);
	//i.e RoomBot/#mibbit
	target = matcher.group(3);
	//i.e I have 3093 clients and 1 servers
	param = matcher.group(4);
}

Would you have done differently?

Related posts:

  1. Recent changes to the Google Chrome extension gallery

  10 Responses to “Regular expression to match raw IRC messages”

  1. [...] reading here: Regular expression to match raw IRC messages | Joshua Lückers Share and [...]

  2. Not sure I understand how it knows how to section it, could you explain? (Never really did this sort of thing)

  3. Thanks for posting this up. I’ve been trying to find one to parse raw IRC commands; Been too busy to write one myself.

    I’m actually writing an IRC client in PHP via PHP CGI, so I’m trying to find the most optimized way to parse each command sent to the client so it can handle it how it needs to. If you have any resources that might help me code my first IRC client, I’d love to hear from you!

    Thanks again for posting this up, man.

  4. $source = ‘PING’, when parsing a standard ping/pong from the protocol (PING :irc.example.com)

  5. Hmm, it seems the regex expression doesn’t seem to parse JOIN messages correctly.

    Group(3) would be null.

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

 
© 2011 Joshua Lückers Suffusion theme by Sayontan Sinha