HTML CSS PHP PERL

Hour 6. Pattern Matching

 
Previous Table of Contents Next

Hour 6. Pattern Matching

What You'll Learn in This Hour:

In the preceding hour, you learned about reading data from files. With that information, combined with your knowledge of scalars, arrays, and operators, you're prepared to manipulate that data to do what you wantalmost. The data in the file might not have an easy-to-use format that can be broken up with a simple split on spaces. You might have lines that contain data you're not interested in and want to edit out.

What you need is the ability to recognize patterns in the input stream, to pick and choose data based on those patterns, and possibly to edit the data to a more usable form. One of Perl's tools for performing these tasks is regular expressions. Throughout this text, regular expression and pattern will be used almost interchangeably.

Regular expressions are almost a language unto themselves. Regular expressions are a formal method of describing patterns to match. In this hour, you'll learn just a little bit of this pattern-matching language.

By the Way

The online documentation has a much deeper (but more terse) description of the full regular expression language used by Perl. You can look at the perlre documentation included with Perl. The topic is so deep that an entire book has been published on regular expressions. This book, highly recommended by the Perl community, is called Mastering Regular Expressions by Jeffrey E. F. Friedl (Sebastopol: O'Reilly, 2002). It is about regular expressions in general, but close attention is paid to Perl.


Regular expressions are used in other programming languages as well, including TCL, JavaScript, and Python. Many of the Unix operating system utilities also use regular expressions. Perl happens to have a very rich set of expressionsvery similar to those used in other systems, but more powerfuland learning them will help you not just in Perl, but in other languages as well.

    Previous Table of Contents Next
    © 2000- NIV