| Chess | Tools | Data | Blog | Poetry | Why? | Wiki | Admin | Logout |

Download


#!/usr/bin/perl -w
# $Revision: 1.10 $
# Luis Mondesi < lemsx1@gmail.com >
#
# DESCRIPTION: CGI to test regular expressions against a string
# USAGE: regex-test 'regex=REGEX&str=string'
# LICENSE: GPL

use strict;
$|++;

eval "use CGI qw/:cgi/";
if ($@)
{
    print STDERR "
ERROR: Could not load the CGI module.
" .
    "       To install this module use:
".
    "       Use: perl -e shell -MCPAN to install it.
".
    "       On Debian just: apt-get install perl-modules 

".
    print STDERR "$@
";
    exit 1;
}

# Args:
my $HELP_MSG="$0 'regex=REGEX&str=string'";
my $REGEX=undef;
my $STR=undef;
my $str = undef; 

################################################################
##                         main ()                             #
################################################################

## globals
my $html = new CGI; # OO interface

print STDOUT (
    $html->header(),
);

#$REGEX=$ARGV[0];
#$STR=$ARGV[1];

#$REGEX="0";
#$STR="0123abc";

_print_form();

if ($html->param())
{
    $REGEX = $html->param('regex');
    chomp($REGEX);
    $REGEX =~ s/
//g;
    $STR = $html->param('str');

    if ( defined($REGEX) and defined($STR) )
    {
        no warnings;
        print STDOUT (
            "<p><font color='#cecece'>Perl Regex: m/$REGEX/g</font></p>
",
            $html->hr(),
        );

        foreach( split(/
/,$STR) )
        {
            if ( $REGEX =~ /\(/ )
            {
                my $count = ($REGEX =~ tr/\(//);
                print STDOUT (
                    $_,
                    "<br />
<font color='#cecece'>regex $REGEX has $count parenthesis</font><br />
"
                );
                print STDOUT (
                    "Matches: ",
                );
                m/($REGEX)/g;
                print STDOUT (
                    "<font color='green'><b>",
                    $1,
                    #,$2,$3,$4,$5,$6,$7,$8,$9,
                    "</b></font>"
                );
                print "<br />
";
            } else {
                m/(.*)($REGEX)(.*)/g;
                print STDOUT (
                    "<font color='red'>$1</font>",
                    "<font color='green'><b>$2</b></font>",
                    "<font color='red'>$3</font>",
                    "<br />
"
                );
            }
        } #end for
    } else {
        print STDERR $HELP_MSG,"
";
    }
}

print STDOUT ( "
",$html->end_html() );

# helper functions
#
sub _print_form
{
    # we are not doing lookups, print form:
    print STDOUT (
        $html->start_html('Regular Expression Tester'),
        $html->h1('Enter Regular Expression:'),
        $html->start_multipart_form()
    );
    print STDOUT (
        $html->p("Regex: "),
        $html->textarea(-name=>"regex",-default=>'',-rows=>10,-columns=>50),
        $html->p("Text: "),
        $html->textarea(-name=>"str",-default=>'',-rows=>10,-columns=>50),
        "
",
        $html->p()
    );
    print STDOUT (
        "
",
        $html->submit(),
        $html->end_form(),
        $html->hr()
    );

}


Advertisement