in reply to Regexp problem matching commands for IRC bot

The problem is that you are using greedy-capture in your regex. ( (.*) ). This will capture as much as possible up to !, so when a message begins with !, $1 ends up containing everything on the line until that point. You can solve this easily by using non-greedy capture. ( (.*?) ).

However, I do wonder why you're doing this completely from scratch, when there are several IRC interfaces available on CPAN.

  • Comment on Re: Regexp problem matching commands for IRC bot

Replies are listed 'Best First'.
Re^2: Regexp problem matching commands for IRC bot
by Eimi Metamorphoumai (Deacon) on Dec 17, 2004 at 20:20 UTC
    That shouldn't be the problem. Yes, it'll try that first, but in order to match the PRIVMSG part, it'll have to back off. My guess would be that it's something like CRLF issues. The best bet to debug it would be to look at exactly what's held in $message, checking for any characters you weren't expecting (trailing spaces? Trailing carriage return? etc), and possibly match against just /^!quit/ or /^!quit\b/, or maybe /^!quit\s*$/. But the advice to use an existing IRC framework like Net::IRC is good.
      Yeah, I also forgot to explain that if you just use the ^ in the regex it does not work, not does just using $, so there might be something in front and back of !quit in $message.
        Have you tried simply printing out the contents of $message?
        print "\$message is '$message'\n";
        That should at least help you figure out why it's not matching.
        Ah, it looks like: <code> $message is ':Admin!Admin@leet-A0B340B0.host.com PRIVMSG #channel :!quit ' <code> Not good.
Re^2: Regexp problem matching commands for IRC bot
by StrifeChild (Novice) on Dec 17, 2004 at 20:18 UTC
    Do you have an example of a better regex to use? I am doing it from scratch because I think it is easier for me to implement my functions, like a Google/Yahoo search. Also it is a good challenge :)