#!/usr/bin/perl -w

use CGI;

# create CGI object
$q          = new CGI;

# extract incoming POST data from STDIN
$text       = $q->param('text');
$expression = $q->param('expression');
$substitution = $q->param('substitution');

chomp($text);
chomp($expression);

$out = $text;
$result = "";



$out = $text;

if ($expression !~ m/[\w{1,}|\.]/ || $expression eq '')
{
	$result = "Unable to handle expression."
}
elsif ($out =~ s/($expression)/$substitution/g )
{
	
	$result = $out;
}
else
{
	$result = "Found no match.";
}


print <<FORM;
Content-type: text/html\n\n


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title>
Regular Exression Tester</title>
<style type="text/css">
\@import "http://people.cs.uchicago.edu/~sterner/cs101/testpl/regex/teaching.css";

.left   { float: left; width: 30em; }
#result { border: solid 1px black; padding: 0.5em; width: 30em; }

</style>
</head>
<body>
<h2>Regular Expression Substitution</h2>
<form method="post" action="http://www.people.cs.uchicago.edu/~sterner/cs101/testpl/regex/regex_sub.pl" id="reg_ex_sub">
<div class="left">
<h4>Text Source:</h4>
<input type="text" name="text" value="$text" size="30" maxlength="30" />
</div>
<div>
<h4>Regular Expression:</h4>
s/<input type="text" name="expression" value="$expression" size="15" maxlength="15" />/
&nbsp;<input type="text" name="substitution" value="$substitution" size="15" maxlength="15" />/g
</div>

<div class="left">
<h4>Replaced:</h4>
</div>
<br clear="all" />
<div class="left">
<span>
<input class="raise" type="text" name="replaced" value="$out" size="30" maxlength="30" />
</span>
</div>
<div class="center"><input type="submit" name="submit" value="Submit" />

</div>

<br />
<span>
<code>
(Substitution is global; does not allow groupings.)
</code>
</span>

</form>
 
</body>
</html>


FORM


