Regular exprssion for IRC messages

nohics nohics nohics at gmail.com
Fri Jul 17 12:59:47 EDT 2009


Hello,

When an IRC client receive a messages, this message in in a special format
defined here: Message format in pseudo
BNF<http://www.irchelp.org/irchelp/rfc/chapter2.html#c2_3_1>(
http://www.irchelp.org/irchelp/rfc/chapter2.html#c2_3_1 ).

For example we can have:

:MyNickName!n=MyUserName at 41.238.129.121 JOIN :#AnIrcChannel
:MyNickName!n=MyUserName at 41.238.129.121 PRIVMSG #AnIrcChannel :Hello here
:MyNickName!n=MyUserName at 41.238.129.121 NICK :TheNewNickName
:holmes.freenode.net 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 = nicknamePattern.search(data)
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):
http://www.developpez.net/forums/d779736/php/langage/regex/format-message-pseudo-bnf-expression-reguliere/#post4494010
Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090717/1bd7667f/attachment.html>


More information about the Python-list mailing list