XML technologies / Regular expressions / Perl
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
Perl appears throughout this book for good reasons. It is widespread, offers a rich regex language, is free and readily available, welcomes beginners, and runs on many platforms, including Windows, Unix, and MacOS.
Some syntax resembles C or other traditional languages, but similarities end there. Perl’s approach to solving problems, The Perl Way, differs greatly from traditional approaches. A script’s broad structure often resembles structured or object-oriented programs, but actual data processing relies heavily on regexes. Regexes play a crucial role in almost every Perl program, I think it is fair to say. This holds for huge packages with tens of thousands of lines as well as a one-liner such as
% perl -pi -e 's{([-+]?\d+(\.\d*)?)F\b}{sprintf "%.0fC", ($1-32)*5/9}eg' *.txtthat replaces Fahrenheit temperatures in *.txt files with Celsius, recalling the first example from Extended introductory examples.
In this chapter
This chapter covers Perl’s regex-related features, using version 5.8.8: its dialect and regex operators. Regex matters are explained from the ground up, but some general Perl familiarity is assumed. Working through Extended introductory examples may initially suffice. Some points are only mentioned briefly, and unrelated language features are barely explained. Keep Perl’s documentation or the book Programmieren mit Perl from O’Reilly handy.
Perhaps more important than prior Perl knowledge is a desire to learn more about Perl. This chapter is demanding. I am not writing a Perl textbook, so I can focus on interesting subjects without building a coherent course or excluding topics too hard for beginners. The common thread is deep understanding. Some topics are intricate and may not be immediately clear. Read the chapter once for the overall picture, then use it as a reference.
This preview clarifies the chapter’s structure:
Under Perl’s regex dialect we examine Perl’s many metacharacters and extra regex-literal features.
Perl-specific regex topics covers Perl aspects especially relevant to regexes, including dynamic scope and regex context, always in relation to regular expressions.
Regexes are useful only when applied. The following sections examine operators that use them in sometimes magical ways:
The qr/.../ operator and regex objects
The match operator
The substitution operator
The split operatorUnder Unusual techniques with Perl regex extensions we explore advanced, sometimes unusual Perl extensions, such as running arbitrary Perl code from within a regex.
Efficiency in Perl addresses a topic relevant to every programmer. Perl uses a traditional NFA, so techniques from The art of writing regular expressions apply directly. Perl also has its own characteristics affecting how, and especially how fast, regex programs run.
Perl in earlier chapters
Perl is used or mentioned throughout most of this book:
Extended introductory examples introduces Perl with many regex examples.
Features and dialects briefly traces Perl’s history (see Perl evolves too) and covers relevant topics such as character encodings, including Unicode (see Character encodings), matching modes (see Regex modes), and a broad survey of metacharacters (see Common metacharacters).
How regex engines work details the traditional NFA used by Perl. For Perl users, How regex engines work is central.
Practical regex techniques provides many examples illustrating How regex engines work. Most programs are Perl, but the others also cover relevant topics.
The art of writing regular expressions matters for Perl program efficiency.
Earlier Perl examples were often deliberately simple, keeping the main point clear and helping readers unfamiliar with Perl. This chapter’s examples use idiomatic Perl.
German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln