Perl/Regex Tester

From Devpit
Jump to: navigation, search

This is a handy way to test a regular expression you aren't sure about. You can put as many test cases as you want and the output tells you at a glance when one fails.

#!/usr/bin/perl

use strict;
use warnings;


# Put test-cases for your regex in here.  The 0/1 indicates whether the test
# case should match the string that follows.

my $tests = [
        [0, '_L(float)'],
        [0, '_L (float)'],
        [0, 'a _L(float)'],
        [1, 'a_L(float)'],
        [1, '_La(float)'],
        [1, 'yes(float)'],
        [1, 'yes (float)'],
        [1, 'a yes(float)'],
];


# Put the regex you want to test in here.

my $regex = qr/(^|\s)(?!_L\s*\()\S+\s*\(float\)/s;


foreach my $test (@$tests) {
        if($test->[1] =~ $regex) {
                if($test->[0]) {
                        print "$test->[1] okay\n";
                } else {
                        print "$test->[1] FAILED!\n";
                }
        } else {
                if($test->[0]) {
                        print "$test->[1] FAILED!\n";
                } else {
                        print "$test->[1] okay\n";
                }
        }
}