|
|
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.
Comments
|
Sandeep | Posted at 11:09pm on Monday, September 17th, 2007 | You should provide some practicle examples buddy |
Ankit | Posted at 1:45pm on Monday, September 24th, 2007 | I agree with you sandeep |
Spelling Monster | Posted at 11:18am on Monday, November 5th, 2007 | I think Sandeep should learn how to spell "practical". |
John W. Krahn | Posted at 6:02pm on Wednesday, January 16th, 2008 | You say: "Note how the curly braces have to be balanced."
It depends on how you do it:
$ perl -le'print "apples pears oranges" =~ m{pears}'
1
$ perl -le'print "apples pears oranges" =~ m{pears{'
Search pattern not terminated at -e line 1.
$ perl -le'print "apples pears oranges" =~ m}pears}'
1 |
Ankush | Posted at 3:59am on Monday, March 3rd, 2008 | To be frank, quite a waste of time. Just type "Perl Regular Expressions" on Google and you will find much much better articles. |
abcd | Posted at 6:51am on Thursday, March 27th, 2008 | thank u ankush |
perlmonger | Posted at 6:48am on Wednesday, April 16th, 2008 | perldoc -q regex |
Comments to date: 7.
|
The Regex Tutor
Try your regular expressions online with the regex tutor.
Suggested Reading
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.
|