Home: Perl Programming Help: Regular Expressions:
possible regex problem, not matching



fatalnix
New User

Oct 19, 2008, 3:08 PM


Views: 12972
possible regex problem, not matching

Hi, I have a strange problem. You see, Though I am aware of modules such as POE::Component::IRC and such, I am writing an IRC bot with IO::Socket because I find it to be much more involved, and I needed something that I had complete control over as well as learn the RFC for IRC and concentrate on improving my regex skills. I was unsure either to put this under beginner or regex so please don't flame me for this, I'm unsure of the true problem but think it may be something to do with it-

I made two scripts, one that takes a regex line into an if statement with an actual example, to check that my regex matches correctly, the regex was simply copied and pasted t ensure they were the same.. The test script returns that the regex in fact does match. but in the irc bot script, when it recieves the data, it does not.

(I looked for any posts regarding rules to see if I'm not supposed to put in links, but did not see anything, so please redirect me if I am mistaken)
I sent it to pastebin so it didnt spam 2 pages of the forum:

http://pastebin.com/d2e93505c

I will be happy for anyone who tries to help.

Edit: I forgot to mention something possibly important, the end of /motd regex works and the ident one does, but not join or both privmsg's


(This post was edited by fatalnix on Oct 19, 2008, 3:09 PM)


KevinR
Veteran


Oct 19, 2008, 3:17 PM


Views: 12969
Re: [fatalnix] possible regex problem, not matching

this:


Code
	if ($data =~ /^:(\w+)!\w+(\@|\-)[\w\.]+ PRIVMSG $irc_nick :(.*)/ ) { 
my ($nick, $chan, $msg) = ($1, $3, $4);


looks like it should be:


Code
	if ($data =~ /^:(\w+)!\w+(\@|\-)[\w\.]+ PRIVMSG $irc_nick :(.*)/ ) { 
my ($nick, $chan, $msg) = ($1, $2, $3);

-------------------------------------------------


fatalnix
New User

Oct 19, 2008, 3:27 PM


Views: 12968
Re: [KevinR] possible regex problem, not matching

the reason for $1, $3, and $4 is because $2 is either @ or -, since the irc server I'm connected to is sometimes - and sometimes its reported as @, but if I removed the ()'s from (\@|\-) would that still work?

here is an example format of the irc server output in which the join regex would match, if it helps:

:nickname!theiruser@example.host.mydsl.com JOIN :#channel


(This post was edited by fatalnix on Oct 19, 2008, 3:31 PM)


KevinR
Veteran


Oct 19, 2008, 3:46 PM


Views: 12964
Re: [fatalnix] possible regex problem, not matching

That would match against another regexp:


Code
	if ($data =~ /^:(\w+)!\w+(\@|\-)[\w\.]+ JOIN :(#\w+)$/ ) { 
my ($nick, $chan) = ($1, $3);
print "CAUGHT JOIN\n";
}


And you notice it uses $1 and $3.

$2 would be the @|- match.
-------------------------------------------------


KevinR
Veteran


Oct 19, 2008, 3:50 PM


Views: 12962
Re: [fatalnix] possible regex problem, not matching

In this regexp:


Code
	if ($data =~ /^:(\w+)!\w+(\@|\-)[\w\.]+ PRIVMSG $irc_nick :(.*)/ ) {  
my ($nick, $chan, $msg) = ($1, $3, $4);


There is no capturing group $4. You have:

(\w+) = $1
(\@|\-) = $2
(.*) = $3

There is no fourth ($4) capturing group.
-------------------------------------------------


fatalnix
New User

Oct 19, 2008, 3:52 PM


Views: 12962
Re: [KevinR] possible regex problem, not matching

Oh yeah, sorry. So, is there any idea as to why these regex's aren't matching at all? the if statement appears to be coming false on the irb bot script, and I have taken the output from the socket on my terminal and put it in my regex testing script and the statement comes to be true..


KevinR
Veteran


Oct 19, 2008, 5:59 PM


Views: 12949
Re: [fatalnix] possible regex problem, not matching


In Reply To
Oh yeah, sorry. So, is there any idea as to why these regex's aren't matching at all? the if statement appears to be coming false on the irb bot script, and I have taken the output from the socket on my terminal and put it in my regex testing script and the statement comes to be true..


I don't know why that is. Maybe ask on www.perlmonks.com and see if anyone has a suggestion.
-------------------------------------------------