my $line = 'Oct 9 08:50:53 mail_server sendmail[30172]: j99FopoN030172: from=, size=0, class=0, nrcpts=0, proto=ESMTP, daemon=MTA, relay=[218.1.114.182]'; if ($line =~ /relay=\[(.*?)\]/) { print $1; }