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?

© 2011 Joshua Lückers Suffusion theme by Sayontan Sinha