Perlfect Solutions

An Introduction to Perl Regular Expressions

What is a perl regex?

In its simplest form a regular expression, or in short a regex or a pattern, is nothing more than a string, ie, a sequence of characters. For example in the expression below "apples pears oranges" =~ /pears/ the string pears constitutes a regex. Generally regexes are enclosed by forward slashes.

What are regexes good for?

As you might be able to guess from the code snippet above regexes can be used to check if one string matches part of another, usually longer, string. In our simple example the string on the left hand side is matched against the regular expression on the right hand side. The =~ is the match operator. The result of this operation is a true value if a match can be found, a false value otherwise. To verify this try the following:

print "apples pears oranges" =~ m/pears/;

It should produce the output 1. If you are paying attention you probably noticed a spurious m preceding the regex. As long as you choose to enclose your regular expressions with forward slashes it is optional. However, you may use it to modify your regex delimiters. The following statements are equivalent:

"apples pears oranges" =~ m|pears|; "apples pears oranges" =~ m{pears}; "apples pears oranges" =~ m"pears";

Note how the curly braces have to be balanced. The reason you might wish to modify your delimiters is readibility. Consider the following:

"55 miles/hour" =~ /\//; "55 miles/hour" =~ m|/|;

Designating the vertical bar as your regex delimiter allows you to use forward slashes within your regular expression without the need to escape them. Compare and contrast this with the first line where the forward slash has a special meaning, a regex delimiter, and hence cannot be used as part of your pattern unless is escaped by being prefixed with a backslash.

Apart from matching, regular expressions can be used to substitute part of a string with another. The syntax for searching and replacing is similar to the syntax for matching which you have already seen

my $planets = "earth, mars, mercury, pluto"; $planets =~ s/earth/zeus/; print $planets;

The expression in the second line will replace earth by zeus withing the string $planets. Notice that the match operator is followed by a single s which stands for substitute.

But that's not all! Regular expressions can be provided as arguments to the split built-in method. Suppose, we wanted to store our planets in an array. Here is a way to do it:

my @planets = split(/, /, $planets)

The first argument we've provided to the split function is a regular expression consisting of a single comma followed by a space. The second argument is the string we want to split. A split will occur at every point the regex matches. In our example there are three positions in the string $planets which match the regex. The fours planets will be returned in an array.

In this article we have touched only the tip of the iceberg that is regular expressions. In a followup to this article we will talk about character classes, conditional matching, extracting matches, modifiers and more.

Save This Page


Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'perlfect'@'' (using password: YES) in /home/content/g/i/o/giorgoszervas/html/comments/comments_include.php on line 6
Connection Error: Access denied for user 'perlfect'@'' (using password: YES)

Like it? Share it!

Post to
Post to

The Regex Tutor

Try your regular expressions online with the regex tutor.

Suggested Reading

Order your copy of Mastering Regular Expressions now! Mastering Regular Expressions covers everything related to perl regular expressions and is useful to novices and experts alike.
Order your copy of Programming Perl now! Programming Perl is the one-stop shop for learning all there is to know about regular expressions and more! A truly indispensable book for any Perl programmer.