in reply to Regex substitute matches second match first?
You're being too greedy. Change:
$line =~ s/.*\]: //;
...to:
$line =~ s/.*?\]: //; # note the ? following .*
That'll stop as soon as it sees the first ], whereas without the non-greedy quantifier ?, it'll slurp in the entire string until it finds the last ]
Here's a way that you can do all of your matching and capture the IP on one line:
if ($line =~ /\[(\d+\.\d+\.\d+\.\d+)\].*Relay access denied/) { my $ip = $1; print "$ip\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Regex substitute matches second match first?
by Marshall (Canon) on May 25, 2016 at 18:02 UTC | |
|
Re^2: Regex substitute matches second match first?
by Linicks (Scribe) on May 25, 2016 at 19:08 UTC | |
|
Re^2: Regex substitute matches second match first?
by Linicks (Scribe) on May 25, 2016 at 17:18 UTC | |
by stevieb (Canon) on May 25, 2016 at 17:24 UTC | |
by Linicks (Scribe) on May 25, 2016 at 17:33 UTC |