summaryrefslogtreecommitdiffstats
path: root/regcheck.pl
blob: aa7929cae46237030f7a30471b1d26a9d7343747 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/perl
use warnings;
use strict;
use File::Basename;

if (@ARGV < 1) {
  print "usage: cat | ", basename($0), " <regexp>\n";
  exit 2;
}

my $pattern = shift;

my $line_counter = 0;
while (<>) {
	$line_counter++;
	my $line = $_;
	chomp($line);

	if (my @matches = $line =~ /$pattern/) {
		if ($#- != 0) {
			my $counter = 0;
			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 {
			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";
	}
}