diff options
-rwxr-xr-x | regcheck.pl | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/regcheck.pl b/regcheck.pl index a89b562..aa7929c 100755 --- a/regcheck.pl +++ b/regcheck.pl @@ -3,26 +3,34 @@ use warnings; use strict; use File::Basename; -if (@ARGV < 2) { - print "usage: ", basename($0), " <regexp> <string>\n"; +if (@ARGV < 1) { + print "usage: cat | ", basename($0), " <regexp>\n"; exit 2; } my $pattern = shift; -for my $string (@ARGV) { - if (my @matches = $string =~ /$pattern/) { - print "\e[0;32mmatches:\e[0m "; +my $line_counter = 0; +while (<>) { + $line_counter++; + my $line = $_; + chomp($line); + + if (my @matches = $line =~ /$pattern/) { if ($#- != 0) { - print "\n"; my $counter = 0; - for my $match (@matches) { - print $counter++.": \"".$match."\"\n"; + if (scalar(@matches) == 1) { + printf "\e[0;32mmatched group\e[0m \e[0;38;5;12m%s\e[0m in line %d (%s)\n", $matches[0], $line_counter, $line; + } else { + printf "\e[0;32mmatched %d groups\e[0m in line %d (%s):\n", scalar(@matches), $line_counter, $line; + for my $match (@matches) { + print "\t".$counter++.": \e[0;38;5;12m".$match."\e[0m\n"; + } } } else { - print "$&\n"; + printf "\e[0;32mmatched\e[0m \e[0;38;5;12m%s\e[0m in line %d (%s)\n", $&, $line_counter, $line; } } else { - print "\e[0;31mno match\e[0m\n"; + #print "\e[0;31mno match\e[0m\n"; } } |