XML technologies / Regular expressions / Extended introductory examples / Pattern matching with regular expressions

(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)

In Perl regular expressions are used in many ways. The simplest use checks whether the text in a variable matches an expression. The following fragment tests the string in $eingabe and reports whether it contains only digits:

if ($eingabe =~ m/^[0­9]+$/) {
   print "nur Ziffern\n";
} else {
   print "nicht nur Ziffern\n";
}

The first line initially looks odd. The regular expression is ˹^[0­9]+$˼; the surrounding m/.../ tells Perl what to do with it. The m stands for matching and denotes the match operator; the slashes delimit the expression. (Note: the m can usually be omitted, and many Perl programmers always omit it: $eingabe =~ /^[0­9]+$/. Personally, I like the m for match, so I usually include it.) The =~ binds the regex to the text being tested, here the contents of $eingabe.

Do not confuse =~ with = or ==: these are entirely different. The operator == compares two numbers ( string comparisons require eq, as we will soon see). The operator = assigns a value to a variable, here $celsius = 20. In contrast, =~ binds a regex to the string being searched. Here, m/^[0­9]+$/ is the pattern applied to $eingabe . Other languages handle this differently; see Features and dialects for examples ⟦U+0002⟧.

You can read the operator as “matches”.

if ($eingabe =~ m/^[0­9]+$/)

then means:

if the text in the variable $eingabe matches ˹^[0­9]+$˼, then ...

The result of $eingabe =~ m/^[0­9]+$/ is true if ˹^[0­9]+$˼ is found in the string $eingabe, otherwise false. The if statement tests this truth value and determines which message to print.

A test for $eingabe =~ m/[0­9]+/ (as before, but without the surrounding caret ⟦U+0003⟧ and dollar sign) would be true if $eingabe contained at least one digit anywhere. The ˹^...$˼ ensures that $eingabe only contains digits.

Now combine the last two examples. Ask the user for a number, read it, and use a regex to check that it contains only digits. If so, convert the entered Celsius temperature to Fahrenheit; otherwise, print an error message.

print "Geben Sie eine Temperatur in Celsius ein: ";
$celsius = <STDIN>;       # liest eine Eingabe-Zeile
chomp($celsius);          # entfernt das Newline von $celsius

if ($celsius =~ m/^[0­9]+$/) {
   $fahrenheit = ($celsius * 9 / 5) + 32; # Fahrenheit berechnen
   print "$celsius C entspricht $fahrenheit F.\n";
} else {
   print "Zahl erwartet, nicht \"$celsius\".\n";
}

In the final print statement, the two quotation marks around $celsius had to be escaped with backslashes. In every programming language, string delimiters can also occur inside the string. Perl treats strings and regexes almost identically here: prefix a backslash. In other languages, such as Java and Python, distinguishing these cases is very important. This is explained further in Metacharacters everywhere. In VB.NET, to include a quotation mark in a quoted string, use ›""‹ instead of ›\"‹‹.

Saving this program in a file named c2f lets us run it:

% perl -­w c2f
Geben Sie eine Temperatur in Celsius ein: 22
22 C entspricht 71.599999999999994316 F.

Oops. Perl’s simple print operator evidently does not handle floating-point numbers very well. Without going into details, printf , meaning print formatted, works better:

printf "%.2f C entspricht %.2f F.\n", $celsius, $fahrenheit;

This changes only how the values are printed, not the values of the variables themselves:

Geben Sie eine Temperatur in Celsius ein: 22 
 22.00 C entspricht 71.60 F.

  

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

back to top