
kiojichi
New User
Sep 4, 2012, 4:22 PM
Post #1 of 3
(1741 views)
|
Assignment problem with regex
|
Can't Post
|
|
Hi All, I'm having an issue getting a value assigned to a variable after a regex match, for an iP address. I'm sure I'm doing something that's clearly wrong, and obvious to most, but I'm new to Perl and I've been working on this for 2 days now. The assignment is just not happening right. Code:
sub block_match { my $line_instance_b = $_[0]; my $ipaddress; if ( $line_instance_b =~ /banned_ip|Found UltraSurf Signature|ip_block / ) { $ipaddress = $1 if $line_instance_b =~ /\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/; print "Found ip address: ", $ipaddress, "in the following line: \n", $line_instance_b; } else { print "Not a block line: \n", $line_instance_b, "\n" } } The line that the regex is matching against is:
1307931062: INIT: banned_ip add 208.67.219.132 for FreeGate In the match and substitution line, it appears that the regex is matching just fine, but when I try to send the matched data to a variable ($ipaddress in this case) it always fails. Is there a better way to do this? Thanks.
|