4.2 Match Delimiters
The regular expression match operator is =~. On the left hand side, we have the target of the match. On the right hand side we have the regular expression along with any modifiers, if any. An example is
$string =~ m/theoretical/
Here, we are looking for the pattern theoretical in $string.
If on the left hand side, the variable over which we are doing the pattern matching is the special variable $_, we need not write the variable as well as the operator =~. Therefore,
$_ =~ m/theoretical/
and
m/theoretical/
mean exactly the same. In addition, if / is used as the delimiter, the use of m is not necessary. Therefore, we can write the previous expression as
/theoretical/
also.
Actually, Perl allows us to use any non-alphanumeric, non-whitespace character as the delimiter if we use m to indicate the match operation. In the specific example, we have shown earlier, any of the following will work instead of m/$pattern/.
m@$pattern@
m#$pattern#
m=$pattern=
m!$pattern!
m~$pattern~
m%$pattern%
m^$pattern^
m*$pattern*
m-$pattern-
m+$pattern+
m:$pattern:
m;$pattern;
m"$pattern"
m|$pattern|
m\$pattern\
m,$pattern,
However, there are some exceptions. For a complete discussion, one should look at the book Mastering Regular Expressions [Fri97]. Here are some of the exceptions. Although we said any non-alphanumeric character will work as the delimiter, the four “natural” delimiters need to be used in complementary pairs. In other words, the following do not work:
m{$pattern{
m[$pattern[
m($pattern(
m<$pattern<
but they work in natural pairs as in the following.
m{$pattern}
m[$pattern]
m($pattern)
m<$pattern>
If we use ? as the delimiter, the pattern matches only once. For example, if the previous example were changed to
Program 4.2
#!/usr/bin/perl
$pattern = shift(@ARGV);
while (<>){
if (m?$pattern?){
print "File $ARGV:";
print "\t$_"; #prints the line
}
}
the result will be
File nature.tex: theoretically satisfying, will be
That is, only the first match will take place.
Finally, if we use the single quote as the delimiter, the pattern behaves like a string delimited with a single quote. That is, variables inside the string are not be interpolated. Specifically, in this example, if we change the pattern matching to
m'$pattern'
Perl looks for the literal presence of the string $pattern in the files.