Pattern Matching: Addressing Portions of Patterns


ADDRESSING PORTIONS OF PATTERNS: #!/usr/bin/perl print "ENTER FIRST, MIDDLE, and LAST NAME: "; $n = <STDIN>; chomp($n); while($n ne "q"){ if($n =~ /^([^ ]*) ([^ ]*) ([^ ]*)$/ ){ print "LEGAL\n\n"; print "FIRST: $1\n"; print "MIDDLE: $2\n"; print "LAST: $3\n"; } else{ print "NOT LEGAL\n\n"; } print "ENTER FIRST, MIDDLE, and LAST NAME: "; $n = <STDIN>; chomp($n); } exit;
EQUIVALENT SCRIPT: #!/usr/bin/perl -w $n = ""; while($n ne "q"){ print "ENTER FIRST, MIDDLE, and LAST NAME: "; $n = <STDIN>; chomp($n); if($n =~ /^([^ ]*) ([^ ]*) ([^ ]*)$/ ){ @ray = ($n =~ /^([^ ]*) ([^ ]*) ([^ ]*)$/ ); print "LEGAL\n\n"; print "FIRST: $ray[0]\n"; print "MIDDLE: $ray[1]\n"; print "LAST: $ray[2]\n"; } else{ print "NOT LEGAL\n\n"; } } exit;
ADDRESSING PORTIONS OF PATTERNS: #!/usr/bin/perl print "ENTER DATE: "; $n = <STDIN>; chomp($n); while($n ne "q"){ if($n =~ /^\d{1,2}([\W])\d{1,2}\1\d{1,2}$/ ){ print "LEGAL\n\n"; } else{ print "NOT LEGAL\n\n"; } print "ENTER DATE: "; $n = <STDIN>; chomp($n); } exit;