InMotion Web Site Hosting
InMotion Hosting Home




Node:Pattern Matching, Next:, Previous:The Simple, Up:Regular Expressions



Pattern Matching

Now that you are familiar with some of the basics of regular expressions, you probably want to know how to use them in Perl. Doing so is very easy. There is an operator, =~, that you can use to match a regular expression against scalar variables. Regular expressions in Perl are placed between two forward slashes (i.e., //). The whole $scalar =~ // expression will evaluate to 1 if a match occurs, and undef if it does not.

Consider the following code sample:

use strict;
while ( defined($currentLine = <STDIN>) ) {
    if ($currentLine =~ /^(J|R)MS speaks:/) {
        print $currentLine;
    }
}

This code will go through each line of the input, and print only those lines that start with "JMS speaks:" or "RMS speaks:".


InMotion Web Hosting Customer Support
M-F 8am - 7pm PST
213-239-0050

More information can be found at the InMotion Hosting Main Site