Freaking Crap, Regex Does Suck

The times they are a-changin’.

This post seems to be older than 15 years—a long time on the internet. It might be outdated.

I spend the better part of two hours working on a regex (regular expression) Perl program for my web programming class. Granted, regexs are very power; but boy are they hard. Download the following code and compile/run with ActivePerl 5.8 or better:

#!C:/perl/bin/perl.exe

#Program: Contact.pl
#Author: Andrew Ferguson
#Copyright Andrew Ferguson 2005. All Rights Reserved.


sub FormatName{
my ($n) = @_;
$n =~ tr/A-Z/a-z/; #Convert everything to lowercase to make it easier to work with
$n =~ s/^(w)/uc($1)/ge; #Capitilze the first letter;
return $n;
}


do{
	print "Enter your first name: ";
	$first_name = <>;
}while($first_name !~ / ^([A-Z]||[a-z])*$ /x); #Make sure only alphas are getting through

$first_name = &FormatName($first_name);
print "$first_name";

do{
	print "Enter your last name: ";
	$last_name = <>;
	
}while($last_name !~ / ^([A-Z]||[a-z])*$ /x);#Make sure only only alphas are getting through

$last_name = &FormatName($last_name);
print "$last_name";

do{
	print "Enter your phone number [(123)555-1212]: ";
	$phone = <>;
}while($phone !~ / ^(?(d{3})?)?(-)?(d{3})(-?)(d{4})$ /x ); #Check syntax of entry and make sure it fits what we want

$phone = "$1-$3-$5";

print "$phonen";



do{
	print "Enter today's date: ";
	$date = <>;
}while($date !~ /^([0-1])?([0-9])/([0-3])?([0-9])/([0-2])?([0-9])?([0-9]{2})$/); #Check syntax of entry and make sure it fits what we want


$_1 = $1-0;
$_3 = $3-0;
$date = "$_1$2/$_3$4/$5$6$7$8";
print "$daten";


do{
	print "Enter the time: ";
	$time = <>;
}while($time !~ /^([0-2])?([0-9]):([0-5])?([0-9]):([0-5])?([0-9])$/); #Check syntax of entry and make sure it fits what we want

$_1 = $1-0;
$_3 = $3-0;
$_5 = $5-0;

$time = "$_1$2:$_3$4:$_5$6";
print "$timen";

do{
	print "Enter your email address: ";
	$email = <>;
}
while($email =~ /(@.*@)|(..)|(@.)|(.@)|(^.)/ || $email !~ /^.+@([?)[a-zA-Z0-9-.]+.([a-zA-Z]{2,3}|[0-9]{1,3})(]?)$/); #Checks to make sure format is correct. Acceptable formats are things like: a.ferg@example.com, andrew@example.com, aferg@256.56.95.127, aferg@example.co.uk, etc

print "$emailn";

print "First name:$first_name
Last name: $last_name
Phone: $phonen
Date: $daten
Time: $timen
Email: $emailn";

open(DAT,">data.txt") || die("Cannot Open File");
print DAT "First name:$first_name
Last name: $last_name
Phone: $phonen
Date: $daten
Time: $timen
Email: $emailn";
close(DAT);
0