Example:
#!/usr/bin/perl -w
use strict;
my(@theQuote,$theLine);
@theQuote = ( "Having an open mind is nothing;",
"the object of opening the mind,",
"as in opening the mouth,",
"is to eventually close it again",
"upon something solid. (G.K. Chesterton)" );
foreach $theLine (@theQuote) {
print "theLine : $theLine\n";
print "="x40 . "\n";
# Literal matches
if ( $theLine =~ /H/ ) {
print "/H/ : $theLine\n";
}
if ( $theLine =~ /open/ ) {
print "/open/ : $theLine\n";
}
# Character Classes
if ( $theLine =~ /[Hh]/ ) {
print "/[Hh]/ : $theLine\n";
}
if ( $theLine =~ / [Ii][ts] / ) {
print "/ [Ii][ts] / : $theLine\n";
}
# Negation of character classes
if ( $theLine =~ /[^a]s/ ) {
print "/[^a]s/ : $theLine\n";
}
if ( $theLine =~ /[^a-z ,;]/ ) {
print "/[^a-z ,;]/ : $theLine\n";
}
# Predefined character classes
if ( $theLine =~ /\wis\w/ ) {
print "/\wis\w/ : $theLine\n";
}
if ( $theLine =~ /\sis\s/ ) {
print "/\sis\s/ : $theLine\n";
}
}
[killbill@reepicheep perlclass]$ ./test14.perl
theLine : Having an open mind is nothing;
========================================
/H/ : Having an open mind is nothing;
/open/ : Having an open mind is nothing;
/[Hh]/ : Having an open mind is nothing;
/ [Ii][ts] / : Having an open mind is nothing;
/[^a]s/ : Having an open mind is nothing;
/[^a-z ,;]/ : Having an open mind is nothing;
/siss/ : Having an open mind is nothing;
theLine : the object of opening the mind,
========================================
/open/ : the object of opening the mind,
/[Hh]/ : the object of opening the mind,
theLine : as in opening the mouth,
========================================
/open/ : as in opening the mouth,
/[Hh]/ : as in opening the mouth,
theLine : is to eventually close it again
========================================
/ [Ii][ts] / : is to eventually close it again
/[^a]s/ : is to eventually close it again
theLine : upon something solid. (G.K. Chesterton)
========================================
/[Hh]/ : upon something solid. (G.K. Chesterton)
/[^a]s/ : upon something solid. (G.K. Chesterton)
/[^a-z ,;]/ : upon something solid. (G.K. Chesterton)
[killbill@reepicheep perlclass]$ ./test14.perl