How to use regular expressions in Perl Part 1: Syntax

How to use regular expressions in Perl Part 1: Syntax


Perl is a language which is commonly used for file processing. Regular expressions provide pattern matching and extraction support. Regular expressions can be used in Perl for matching data, not just within files. Perl’s regular expression support is very extensive and rich.


A Perl interpreter, such as ActiveState Perl. Perl may be installed by default on Linux.


Once you have access to data, such as a filehandle or a text string, you can begin matching, such as the following:

$dataOrFilehandle =~ $regularExpression;


$_ =~ $regularExpression;

The =~ goes in between the data and the regular expression. $_ can be used when iterating through a file, line by line in a while loop, for instance. The regular expression can be compiled for speed and referenced as a scalar variable such as:

my $regularExpression = qr/firstPartOfString.*SecondPartOfString/;
$data =~ $regularExpression;

Regular expressions themselves, when writing them, must be enclosed within characters such as ‘/‘. These can be changed:

$data =~ m!stringToMatch.*secondStringToMatch!;

Using the ‘m‘ to define how the regular expression is enclosed is useful if the characters you are matching are the same as the enclosing characters. For instance, if you are matching for “!” in your data, you may not want to use “!” as the enclosing characters.

The regular expression will evaluate as true for a match, or false for a non-match. You can also evaluate a non-match with:

$data !~ /stringToMatch/;

instead of =~. This can be evaluated in an if statement, for example.