I wasn't entirely sure which of the square brackets you wanted to capture. Just swap instances of
( \[ [^]]+ \] ) \s+
with
\[ ( [^]]+ ) \] \s+
(and vice versa) to suit.
#!/usr/bin/env perl use 5.010; use strict; use warnings; my $s1 = "REMOTE [Mon Jul 16 21:49:33 2012] @@ [ueh1] [TNT] [20490 +] [1916] 0.0- 1.0 sec 0.33 MBytes 2.74 Mbits/sec 6.056 ms 0/ +233 (0%)"; my $s2 = "REMOTE [Mon Jul 16 21:49:34 2012] @@ [pdn1] [SSH] [20499 +] [3] 1.0- 2.0 sec 0.34 MBytes 2.86 Mbits/sec"; my @strings = ($s1, $s2); my $re = qr{ \A REMOTE \s+ \[ ( [^]]+ ) \] \s+ ( @@ ) \s+ ( \[ [^]]+ \] ) \s+ ( \[ [^]]+ \] ) \s+ ( \[ [^]]+ \] ) \s+ \[ ( [^]]+ ) \] \s+ ( .+? ) \s+ sec \s+ ( .+? ) \s+ MBytes \s+ ( .+? ) \s+ Mbits/sec (?> \s+ ( .+? ) \s+ ms \s+ ( .* ) | ) \z }x; for (@strings) { say for ('-' x 60, $_, '-' x 60); m{$re}; say for grep { defined } ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, + $11); }
Output:
$ pm_str_remote_re.pl ------------------------------------------------------------ REMOTE [Mon Jul 16 21:49:33 2012] @@ [ueh1] [TNT] [20490] [1916] +0.0- 1.0 sec 0.33 MBytes 2.74 Mbits/sec 6.056 ms 0/ 233 (0%) ------------------------------------------------------------ Mon Jul 16 21:49:33 2012 @@ [ueh1] [TNT] [20490] 1916 0.0- 1.0 0.33 2.74 6.056 0/ 233 (0%) ------------------------------------------------------------ REMOTE [Mon Jul 16 21:49:34 2012] @@ [pdn1] [SSH] [20499] [3] 1.0 +- 2.0 sec 0.34 MBytes 2.86 Mbits/sec ------------------------------------------------------------ Mon Jul 16 21:49:34 2012 @@ [pdn1] [SSH] [20499] 3 1.0- 2.0 0.34 2.86
-- Ken
In reply to Re: regex optional word match
by kcott
in thread Parsind data in xlsx
by Rahul Gupta
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |