Pattern Matching: Integer Validation


MATCHING SCRIPT: #!/usr/bin/perl $line = "tenacious often tendency attention forgotten attend"; print "LIST: $line\n"; print "---------------------------------\n"; print "BEGINNING OF WORD:\n"; @hold = ($line =~ /\bten[^ ]*/g); foreach $item ( @hold ){ print "$item\n"; } print "---------------------------------\n"; print "END OF WORD:\n"; @hold = ($line =~ /[^ ]*ten\b/g); foreach $item ( @hold ){ print "$item\n"; } print "---------------------------------\n"; print "INSIDE WORD:\n"; @hold = ($line =~ /[^ ]*\Bten\B[^ ]*/g); foreach $item ( @hold ){ print "$item\n"; } exit; EXCLUSION GROUPS: [^eE] CHARACTER-RANGE ESCAPE SEQUENCES: \d Any digit [0-9] \D Anything other than a digit [^0-9] \w Any word character [_0-9a-zA-Z] \W Anything not a word character [^_0-9a-zA-Z] \s Whitespace [ \r\t\n\f] \S Anything other than whitespace [^ \r\t\n\f] MATCHING ANY CHARACTER: .* MATCHING SPECIFIED OCCURRENCES: /de{1,3}f/ def, deef, deeef /de{3}f/ deeef /de{3,}f/ deeef, deeeef, deeeeef, etc. /de{0,2}f/ df, def, deef .{3} three of any letter SPECIFYING CHOICES: /def|ghi/ def OR ghi [a-z]+|[0-9]+ matches one or more lowercase letters or one or more digits SIMPLE INTEGER VALIDATION PROGRAM: #!/usr/bin/perl print "ENTER NUMBER: "; $n = <STDIN>; chomp($n); while($n ne "q"){ if($n =~ /^\d+$/ ){ print "LEGAL\n\n"; } else{ print "NOT A NUMBER\n\n"; } print "ENTER NUMBER: "; $n = <STDIN>; chomp($n); } exit;