Checking the Syntax - Page 3
February 9, 2001
Before we go on to more complicated patterns, let's just have a
quick look at that syntax. As we noted previously, a lot of
Perl's operations take $_ as a default argument, and
regular expressions are one such operation. Since we have the
text we want to test in $_, we don't need to use the
=~ operator to 'bind' the pattern to another string.
We could write the above even more simply:
$_ = "Nobody wants to hurt you... 'cept, I do
hurt people sometimes, Case.";
if (/people/) {
print "Hooray! Found the word 'people'\n";}
[Lines 1 and 2 above are one line. They have been split for
formatting purposes.]
Alternatively, we might want to test for the pattern not matching
- the word not being found. Obviously, we could say unless
(/people/), but if the text we're looking at isn't in
$_, we may also use the negative form of that
=~ operator, which is !~. For example:
#!/usr/bin/perl
# nomatch.plx
use warnings;
use strict;
my $gibson =
"Nobody wants to hurt you... 'cept,
I do hurt people sometimes, Case.";
if ($gibson !~ /fish/) {
print "There are no fish in William Gibson.\n";
}
[Lines 6 and 7 above are one line. They have been split for
formatting purposes.]
True to form, for cyberpunk books that don't regularly involve
fish, we get the result.
>perl nomatch.plx
There are no fish in William Gibson.
>
Literal text is the simplest regular expression of all to look
for, but we needn't look for just the one word - we could look
for any particular phrase. However, we need to make sure that we
exactly match all the characters: words (with correct
capitalization), numbers, punctuation, and even whitespace:
#!/usr/bin/perl
# match2.plx
use warnings;
use strict;
$_ = "Nobody wants to hurt you... 'cept,
I do hurt people sometimes, Case.";
if (/I do/) {
print "'I do' is in that string.\n";
}
if (/sometimes Case/) {
print "'sometimes Case' matched.\n";
}
[Lines 5 and 6 above are one line. They have been split for
formatting purposes.]
Let's run this program and see what happens:
>perl match2.plx
'I do' is in that string.
>
The other string didn't match, even though those two words are
there. This is because everything in a regular expression has to
match the string, from start to finish: first "sometimes", then a
space, then "Case". In $_, there was a comma before
the space, so it didn't match exactly. Similarly, spaces inside
the pattern are significant:
#!/usr/bin/perl
# match3.plx
use warnings;
use strict;
my $test1 = "The dog is in the kennel";
my $test2 = "The sheepdog is in the field";
if ($test1 =~ / dog/) {
print "This dog's at home.\n";
}
if ($test2 =~ / dog/) {
print "This dog's at work.\n";
}
This will only find the first dog, as Perl was looking for a
space followed by the three letters, 'dog':
>perl match3.plx
This dog's at home.
>
So, for the moment, it looks like we shall have to specify our
patterns with absolute precision. As another example, look at
this:
#!/usr/bin/perl
# match4.plx
use warnings;
use strict;
$_ = "Nobody wants to hurt you... 'cept, I do
hurt people sometimes, Case.";
if (/case/) {
print "I guess it's just the way I'm made.\n";
} else {
print "Case? Where are you, Case?\n";
}
[Lines 7 and 8 above are one line. They have been split for
formatting purposes.]
> perl match4.plx
Case? Where are you, Case?
>
Hmm, no match. Why not? Because we asked for a small 'c' when we
had a big 'C' - regexps are (if you'll pardon the
pun) case-sensitive. We can get around this by asking Perl to
compare insensitively, and we do this by putting an 'i' (for
'insensitive') after the closing slash. If we alter the code
above as follows:
if (/case/i) {
print "I guess it's just the way I'm made.\n";}
else { print "Case? Where are you, Case?\n";}
[Lines 3 and 4 above are one line. They have been split for
formatting purposes.]
then we find him:
>perl match4.plx
I guess it's just the way I'm made.
>
This 'i ' is one of several modifiers that we can
add to the end of the regular expression to change its behavior
slightly. We'll see more of them later on.
Regular Expressions - Page 2
Beginning Perl
Interpolation - Page 4
|