|
|
Listing C.22 shows you how to perform various pattern matches which leverage the techniques and concepts that you've seen thus far.
Listing C.22 multiMatch1.pl
my($line1) = "123"; my($line2) = "abcde"; my($line3) = "x = 3; # this is comment"; my($line4) = "123-45-6789"; my($left) = ""; my($right) = ""; if( $line1 =~ /^\d+$/ ) { print "$line1 contains only digits\n"; } if( $line2 =~ /^[a-zA-Z]$/ ) { print "$line2 contains only letters\n"; } if( $line3 =~ /#/ ) { ($left, $right) = split(/#/, $line3); print "$line3 contains a comment: $right\n"; } $line3 =~ s/^\s+//; if( $line3 =~ /^#/ ) { print "$line3 is a comment line\n"; } if( $line4 =~ /^(\d\d\d)-(\d\d)-(\d\d\d\d)$/ ) { print "$line4 has a SSN format\n"; }
You can launch the Perl script multiMatch1.pl in Listing C.22 from the command line as follows,
perl -w multiMatch1.pl
and the output is as follows:
123 contains only digits x = 3; # this is comment contains a comment: this is comment 123-45-6789 has a SSN format
|
|