BlueInk has asked for the wisdom of the Perl Monks concerning the following question:

I'm Coding the following for xchat with the perl plug-in
#!/usr/bin/perl -w use Geo::IP; use strict; Xchat::register( "Whois Addon", "0.5", "A Geop and ip resolver for irc +op's for the whois command", "" ); Xchat::hook_server('378', \&parse_line); my $servertabname = "Station51"; sub parse_line { my ($line); $line = shift (@_); Xchat::print("test1",,$servertabname); if ($line =~ m/:.+\@.+\s([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0- +9]{1,3})/){ Xchat::print("test2",,$servertabname); my $ip = $1 ; Xchat::print("Is From ".countryname($ip)."",,$servertabname); } Xchat::print("test3",,$servertabname); } sub countryname{ my ($ip) =@_ ; my $gi = Geo::IP->open("/home/m/Desktop/Test Stuff/GeoIP.dat", GEOIP_S +TANDARD); return $gi->country_name_by_addr($ip); }
The input is as follow's
random.ircserv.net 378 ircop Blueink :is connecting from *@78.273.80.2 +3 78.273.80.23
how ever with the print statements the out put is
test1 test3
Which tell's me the regex is not being accepted does any one have any idea why this is ??? Thanks -- Blueink

Replies are listed 'Best First'.
Re: Frustration With regex
by kennethk (Abbot) on Sep 16, 2009 at 23:08 UTC
    Are you sure parse_line is getting passed what you think it is? The following code behaves as I would expect (regex unchanged):

    #!/usr/bin/perl use strict; use warnings; while (<DATA>) { if (/:.+\@.+\s([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/) { print $1; } } __DATA__ random.ircserv.net 378 ircop Blueink :is connecting from *@78.273.80.2 +3 78.273.80.23
Re: Frustration With regex
by Corion (Patriarch) on Sep 17, 2009 at 07:05 UTC

    Also, for parsing IRC mode lines, consider looking at Parse::IRC to split up the line into its interesting parts.

Re: Frustration With regex
by grizzley (Chaplain) on Sep 21, 2009 at 07:37 UTC

    Better change order of those lines:

    if ($line =~ m/:.+\@.+\s([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3 +})/) { Xchat::print("test2",,$servertabname); my $ip = $1;

    to

    if ($line =~ m/:.+\@.+\s([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3 +})/) { my $ip = $1; Xchat::print("test2",,$servertabname);

    If there is any regexp inside Xchat::print() method, it will reset $1. Maybe this is your case?

    BTW. [0-9] can be written as \d