Regular exprssion for IRC messages

nohics nohics nohics at
Fri Jul 17 12:59:47 EDT 2009


When an IRC client receive a messages, this message in in a special format
defined here: Message format in pseudo
BNF<>( ).

For example we can have:

:MyNickName!n=MyUserName at JOIN :#AnIrcChannel
:MyNickName!n=MyUserName at PRIVMSG #AnIrcChannel :Hello here
:MyNickName!n=MyUserName at NICK :TheNewNickName 372 UnNickName :- a message here ...

I want to transforme the message format defined in the irc protocole in the
last link, to a regular expression to get the nickname, the username, the
host, the commad, it's arguments if exist, and the message after ":"

I'm programming in python and I have tried this regular expression:

^(:(\w+)(![^ \r\n]+)?(\@[^ \r\n]+)? )?([a-zA-Z]+|\d\d\d)(( [^: \r\n][^
\r\n]*)+)( :[^\r\n]*)?

Python code:

nicknamePattern = re.compile(r'^(:(\w+)(![^ \r\n]+)?(\@[^ \r\n]+)?
)?([a-zA-Z]+|\d\d\d)(( [^: \r\n][^ \r\n]*)+)( :[^\r\n]*)?')
matches =
if matches != None:  print matches.groups()

But it doesn't seems to work all the time. So can you help me to make the
correct regular expression please ?

You can see some example of messages working or not with this pattren (when
I try to connect to an irc server):
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list