XML technologies / Regular expressions / Extended introductory examples / About the examples
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
This chapter explores regexes through typical problems: validating input, handling email headers, and converting plain text to HTML. As I build programs, I think aloud about their regexes. We will use constructs and features unknown to egrep . We will also take detours to other important topics.
At the chapter’s end and later in the book, examples use various languages, including PHP, Java and Visual Basic .NET, but most use Perl. These languages, and many others, use regexes in more complex ways than egrep, so every example teaches something new. I begin with Perl mainly because regexes are integrated into the language. Its compact data-handling constructs also simplify the inevitable surrounding code, letting us focus on regexes.
To demonstrate one such construct, recall the ResetSize/SetSize example from Solving practical problems. I used this line of Perl:
% perl -0ne '⟦U+001F⟧print "$ARGV\n" if s/ResetSize//ig != s/SetSize//ig⟦U+001F⟧' *You need not understand it yet; I only wanted to impress you with the solution’s brevity.
I like Perl, but we should not become too absorbed in it: this book focuses on regular expressions. As one professor told first-year students, “You are learning computer science, but we will use Pascal.” Pascal is a traditional language originally designed for teaching. I owe this analogy to William F. Maton and his professor.
This chapter assumes no Perl knowledge, so I explain enough for the examples. Under Perl, discussing Perl’s intricate ⟦U+0003⟧ details, some familiarity is assumed. Even programmers experienced in other languages may find Perl strange: it is compact, with few characters carrying much meaning. The examples therefore avoid its trickiest ⟦U+0003⟧ constructs. That does not make them bad, even if an experienced Perl programmer would write differently. They aim to resemble pseudocode while remaining executable Perl. But we will see some spectacular regex applications.
A short introduction to Perl
Perl is a powerful interactive scripting language developed by Larry Wall in the late 1980s. It borrows many ideas from other languages. Its text and regex handling comes from awk and sed, themselves unlike traditional languages such as C or Pascal.
Perl exists for DOS/Windows, MacOS, OS/2, VMS, Unix, and many other systems. Its affinity for text processing makes it particularly ⟦U+0002⟧ popular for web programming. See perl.org ⟦U+0002⟧ for information on obtaining Perl for your system.
This book covers Perl 5.8, but examples work with every version after 5.005.
Start with a simple problem:
$celsius = 30;
$fahrenheit = ($celsius * 9 / 5) + 32; # Fahrenheit berechnen
print "$celsius C ist $fahrenheit F.\n"; # beide Temperaturen ausgebenRunning this program produces:
30 C ist 86 F.Simple variables such as $fahrenheit and $celsius always begin with a dollar sign and can hold numbers or any amount of text; this example uses only numbers. Comments start with # and continue to the line’s end.
Programmers familiar with C, C#, Java, or VB.NET may be surprised to see variables inside quoted strings. Within the string "$celsius C ist $fahrenheit F.\n", each variable is replaced by its value, called interpolation in Perl. The \n is the newline character.
Perl’s control structures resemble those of many other languages:
$celsius = 20;
while ($celsius <= 45)
{
$fahrenheit = ($celsius * 9 / 5) + 32; # Fahrenheit berechnen
print "$celsius C ist $fahrenheit F.\n";
$celsius = $celsius + 5;
}The part controlled by while executes while the condition, here $celsius <= 45, is true.
Save this in a file named temps to run it directly from the command line:
% perl -w temps
20 C ist 68 F.
25 C ist 77 F.
30 C ist 86 F.
35 C ist 95 F.
40 C ist 104 F.
45 C ist 113 F.The -w option is unnecessary and not directly regex-related. It asks Perl to inspect the program more closely and warn about questionable behavior, such as uninitialized variables; variables need not be declared in Perl. It appears mainly because always using it is a good habit.
That is enough to begin. Next we will explore using regexes in 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