|
|
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 |
Jay | Posted at 11:09am on Monday, June 23rd, 2008 | I agree with Ankush, we get better articles in google. You need to update this article a lot more. |
mweb | Posted at 6:53am on Sunday, September 21st, 2008 | @Ankush, et al.: If you type "Perl Regular Expressions", you get here, too. Get off the author's back. If you're the guy who's using perldoc to get answers, you likely need the owl book anyway. The last sentence is "In this article we have touched only the tip of the iceberg". Read the article before being such a p*ssy. |
ITFarmer | Posted at 8:01am on Thursday, October 2nd, 2008 | #People, Don't complain about free information, Next time, #the author may choose to share NOTHING with you. For #Example:
sub Complain {
my ($Complaint) = @_;
#See if complaint has any "Word Characters"
# i.e. [0-9a-zA-Z_]
# If $Complaint is empty, continue posting free
# articles online
if ($Complaint =~ m/w/ig){
$YouGetNothingFree = 1;
}
return ($YouGetNothingFree, $ThinkPeople);
} |
Don | Posted at 1:07pm on Thursday, October 9th, 2008 | Do any of you know to express in PERL the following URL? I have tried various ways, including / and haven't found one that works yet. The URL:
http://www.domain.com/uhtbin/cgipgm.exe/ABC/0/0/99
Thanks for any help you might be able to provide, including other suggestions for info on this. |
Don | Posted at 1:10pm on Thursday, October 9th, 2008 | It looks like, in the commented I just posted, that it omitted the back slash in my comment about trying different ways, including "back slash, forward slash" to express a "back slash." |
vcao | Posted at 1:49am on Wednesday, April 1st, 2009 | "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. "
http://www.vcao.net |
Nitin Tiwari | Posted at 2:59am on Tuesday, November 24th, 2009 | NIce Article |
IQ | Posted at 4:20am on Monday, December 14th, 2009 | Worth Reading |
POPSNAIL | Posted at 11:49pm on Sunday, January 17th, 2010 | OK you can try this:
http://www.popsnail.com/artciles/guide-how-to-convert-dvd-to-mp4-video-to-mp4-with-cucusoft-ultimate-dvd-video-converter-suite.html
http://www.popsnail.com/video-converters/cucusoft-ultimate-dvd-video-converter-suite-6-2-98.html
Maybe it's can help you! |
alextony | Posted at 2:01am on Wednesday, February 3rd, 2010 | OK....You can try this converter software:
http://cucusoft-ultimate-dvd-converter.software.rating5.com/
http://www.Rating5.com |
Rohit | Posted at 7:07am on Wednesday, February 24th, 2010 | Hi this forum is really good. I am a biginer and I have a question can we also compare the contents of two different folders in terms of their .exe, .dll, .xml files and may be return a warning if the contents of both are not simmiler. If yes please let me know how can it be done. Thanks a lot for your help!! |
Rohit | Posted at 7:08am on Wednesday, February 24th, 2010 | Hi this forum is really good. I am a biginer and I have a question can we also compare the contents of two different folders in terms of their .exe, .dll, .xml files and may be return a warning if the contents of both are not simmiler. If yes please let me know how can it be done. Thanks a lot for your help!! |
Rohit | Posted at 7:09am on Wednesday, February 24th, 2010 | Hi this forum is really good. I am a biginer and I have a question can we also compare the contents of two different folders in terms of their .exe, .dll, .xml files and may be return a warning if the contents of both are not simmiler. If yes please let me know how can it be done. Thanks a lot for your help!! |
SP | Posted at 11:19pm on Sunday, April 4th, 2010 | Thanks |
sathya | Posted at 11:18pm on Monday, December 13th, 2010 | i want to search for a pattern (say string1) in a file;say it occurs in line no:r, i want to replace a string(say , replace string2 with string3) in line no:r+2; and i want to do it recursively for the whole file. is there any way to do this with perl script? |
Bob | Posted at 5:48am on Tuesday, April 5th, 2011 | good article. I need a perl regex to match russian website. |
MoralesRosalind32 | Posted at 3:31pm on Friday, February 3rd, 2012 | Different people all over the world get the loans from various banks, because it is comfortable and fast. |
Comments to date: 24.
|
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.
|