Cover V05, I05
Article
Listing 1
Sidebar 1
Sidebar 2

may96.tar


Sidebar: Perl Regular Expressions

SelectNews.pl uses the line

if( $LoginName =~ /$NameRegExp/ ) . . .

to decide if the user should be shown the news in a directory. If the expression between the forward slashes is in $LoginName, then the statement is true. For example, if $LoginName is abcdefg then

if( $LoginName =~ /cde/ )

is true. To make the line true only when $LoginName is cdf, then write

if( $LoginName =~ /^cde$/ )

The ^ is a metacharacter that isn't taken literally. Rather, it means the beginning of the line, much as is does in the shell. Similarly, $ means the end of the line.

Perl metacharacters don't always have the same meaning to the shell. For example, a+ to Perl means one or more successive a's (a, aa, aaa, aaaa, etc.), because + is a multiplier to Perl. To the shell, + isn't a metacharacter, so a+ is taken literally.