Simple visualizer for perl Regexes
2013-11-02 perl regexI played a bit with way how to locate position of match after successful regex match. The example below shows how to visualize matched captures on a line.
In the DATA section of the script below, there is number of example entries from a log. The regex defined at the beginning of the script has defined three captures.
use 5.16.3;
my $re = qr/^checksession\s+ok:(\d+).*?(fail1:(\d+))?$/;
while(<DATA>) {
chomp;
unless(/$re/) {
say "[ ]$_" ;
next;
}
my $marks = " " x length($_);
while(/$re/g) {
for my $nn (1 .. $#-) {
my $start = $-[$nn];
my $len = $+[$nn] - $-[$nn];
substr($marks,$start,$len) = $nn x $len;
}
}
say "[M]$_\n $marks";
}
__DATA__
checksession ok:6178 avg:479 avgnet:480 MaxTime:18081 fail1:19
checksession ok:6178 avg:479 avgnet:480 MaxTime:18081
checksession ok:6178 avg:479 avgnet:480 MaxTime:18081 fail1:2000
checksession ok:10 avg:479 avgnet:480 MaxTime:18081 fail1:15
Each of lines is printed out with [M]
or [ ]
prefix to show whether the regex
matched. Next line shows placement of the captures.
[M]checksession ok:6178 avg:479 avgnet:480 MaxTime:18081 fail1:19
1111 22222233
[M]checksession ok:6178 avg:479 avgnet:480 MaxTime:18081
1111
[M]checksession ok:6178 avg:479 avgnet:480 MaxTime:18081 fail1:2000
1111 2222223333
[M]checksession ok:10 avg:479 avgnet:480 MaxTime:18081 fail1:15
11 22222233