Home arrow Perl Programming arrow Page 4 - Perl 101 (part 6) - The Perl Toolbox

Aardvark, Anyone? - Perl

This time, Perl 101 visits some of Perl's more useful in-builtfunctions, and teaches you the basics of pattern matching and substitution.Also included is a list of common string and math functions, together withexamples of how to use them.

  1. Perl 101 (part 6) - The Perl Toolbox
  2. Expressing Yourself
  3. Engry Young Men
  4. Aardvark, Anyone?
  5. Needles In Haystacks
  6. Slice And Dice
  7. Going Backwards
  8. Math Class
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 6
August 30, 2000

print this article


The example you just saw was pretty cut-and-dried - decide search term, decide replacement term, shove both into Perl script, bang bang and Bob's your uncle. But what happens if you need to replace a class of words, rather than a single word? If, for example, you need to replace not just the letter "a", but words beginning with one or more "a" and ending with a "k"...like "aardvark", for example?

The special characters you'll use to modify your pattern are called "metacharacters", which is another of those words that sounds impressive but means absolutely nothing useful. We'll explain some of them here, and point you to a resource for more information a little further down.

Two of the more useful metacharacters in Perl are the "+" and "*" characters, which match "one or more" instances and "zero or more" instances of the preceding pattern respectively.

For example,


would match "book", and "booo" but not "bottle", while


would match all of "bottle", "bog", "book", and "booo". One common use of Perl pattern-matching is to remove unnecessary blank spaces from a block of text. This is accomplished using the "\s" metacharacter, which matches whitespace, tab stops and newline characters. The simple substitution

/\s+/ /

would "substitute zero or more occurrences of whitespace with nothing"

Perl also has two important "anchor" characters, which allow you to build patterns which specify which characters come at the beginning or end of a string. The ^ character is used to match the beginning of a string, while the $ character is used to match the end. So,


would match "hello" and "house", but not "shop"



would match "dig" and "bog", but not "gold" or "eagle"

And you can even specify more than one pattern to match with the | operator.


would return true if the string contained either "the" or "a".

Obviously, regular expressions is a topic in itself - if you're interested, we've put together a well-written, comprehensive guide to the topic at So What's A $#!%% Regular Expression, Anyway?!

This article copyright Melonfire 2000. All rights reserved.

>>> More Perl Programming Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: