Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Regular Expressions 2005.pdf
Скачиваний:
95
Добавлен:
17.08.2013
Размер:
25.42 Mб
Скачать

Regular Expressions in Perl

Figure 26-31

How It Works

The key part of xModifier.pl is how the content of the m// operator is laid out in the code. Notice in the last of the following lines that the x modifier is specified. That means unescaped whitespace inside the paired forward slashes of m// is ignored. Also, any characters from # to the end of a line are treated as comments.

This allows the pattern to be spread over several lines, and on each line, a comment can be added that explains the purpose of the component of the regular expression pattern on that line:

if ($myTestString =~

m/\d{5} # Match five numeric digits

(-\d{4})? # Optionally match a hyphen followed by four numeric digits /x)

Escaping Metacharacters

When you want to literally match characters that are used as metacharacters, you must escape the metacharacter using a preceding backslash character.

The following table summarizes the escaping needed for commonly used metacharacters, assuming that paired forward slashes are used to delimit a regular expression pattern.

Escaped Metacharacter

Unescaped Metacharacter

 

 

\/ (backslash followed

/ (forward slash)

by a forward slash)

 

\?

?

\*

*

\+

+

In Perl, because you can specify the delimiters for a regular expression pattern, the use of escaping can vary depending on what delimiter you have specified, as you will see in the next example.

701

Chapter 26

Try It Out

Using Escaped Metacharacters

1.Type the following code in your chosen text editor:

#!/usr/bin/perl -w use strict;

my $myTestString = “http://www.w3.org/”; print “The test string is ‘$myTestString’.\n”;

print “There is a match.\n\n” if ($myTestString =~ m/http:\/\/.*/); print “The test string hasn’t changed but the pattern has.\n”;

print “Also the delimiter character is now paired ‘!’ characters.\n”; print “There is a match.\n\n” if ($myTestString =~ m!http://!);

print “The test string hasn’t changed and the pattern is the original one.\n”; print “Also the delimiter character is still paired ‘!’ characters.\n”;

print “There is a match.\n\n” if ($myTestString =~ m!http:\/\/!);

2.

3.

Save the code as EscapedMetacharacters.pl.

Run the code, and inspect the results, as shown in Figure 26-32.

Figure 26-32

How It Works

The first line assigns a URL (for the World Wide Web Consortium) to the $myTestString variable. Notice that the URL contains forward slash characters, as many URLs do:

my $myTestString = “http://www.w3.org/”;

The test string is output for the user’s information:

print “The test string is ‘$myTestString’.\n”;

If there is a match in the test string for the specified pattern, a message is displayed. Notice how the pattern is constructed. Each forward-slash character is escaped by a preceding backslash character. If you try to run the code with the pattern http://.* but fail to escape the forward slashes, an error message will be displayed:

print “There is

a match.\n\n” if ($myTestString =~ m/http:\/\/.*/);

print “The test

string hasn’t changed but the pattern has.\n”;

print “Also the

delimiter character is now paired ‘!’ characters.\n”;

print “There is

a match.\n\n” if ($myTestString =~ m!http://!);

print “The test

string hasn’t changed and the pattern is the original one.\n”;

print “Also the

delimiter character is still paired ‘!’ characters.\n”;

print “There is

a match.\n\n” if ($myTestString =~ m!http:\/\/!);

 

 

702