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

Regular Expressions in Perl

Figure 26-17

How It Works

The test string is assigned to the variable $myString:

my $myString = “I attended a Star Training Company training course.”;

The variable $oldString is used to hold the original value for later display:

my $oldString = $myString;

The first occurrence of the character sequence Star in the test string is replaced by the character sequence Moon:

$myString =~ s/Star/Moon/;

The user is informed of the original and replaced strings:

print “The original string was: \n’$oldString’\n\n”;

print “After replacement the string is: \n’$myString’\n\n”; if ($oldString =~ m/Star/)

{

print “The string ‘Star’ was matched and replaced in the old string”;

}

Using s/// with the Global Modifier

Often, you will want to replace all occurrences of a character sequence in the test string. The example of the Star Training Company earlier in this book is a case in point. To specify that all occurrences of a pattern are replaced, the global modifier, g, is used.

To achieve global replacement, you write the following:

$myTestString =~ s/pattern/replacementString/g

The g modifier after the third forward slash indicates that global replacement is to take place.

Try It Out

Using s/// with the Global Modifier

1.Type the following code in a text editor:

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

679

Chapter 26

print “This example uses the global modifier, ‘g’\n\n”;

my $myTestString = “Star Training Company courses are great. Choose Star for your training needs.”;

my $myOnceString = $myTestString; my $myGlobalString = $myTestString; my $myPattern = “Star”;

my $myReplacementString = “Moon”;

$myOnceString =~ s/$myPattern/$myReplacementString/; $myGlobalString =~ s/$myPattern/$myReplacementString/g; print “The original string was ‘$myTestString’.\n\n”;

print “After a single replacement it became ‘$myOnceString’.\n\n”; print “After global replacement it became ‘$myGlobalString’.\n\n”;

2.Save the code as GlobalReplace.pl.

3.Run the code and inspect the results, as shown in Figure 26-18. Notice that without the g modifier, only one occurrence of the character sequence Star has been replaced. With the g modifier present, all occurrences (in this case, there are two) are replaced.

Figure 26-18

How It Works

The test string is assigned to the variable $myTestString:

my $myTestString = “Star Training Company courses are great. Choose Star for your

training needs.”;

The value of the original test string is copied to the variables $myOnceString and $myGlobalString:

my $myOnceString = $myTestString;

my $myGlobalString = $myTestString;

The pattern Star is assigned to the variable $myPattern:

my $myPattern = “Star”;

The replacement string, Moon, is assigned to the variable $myReplacementString:

my $myReplacementString = “Moon”;

680

Regular Expressions in Perl

One match is replaced in $myOnceString:

$myOnceString =~ s/$myPattern/$myReplacementString/;

All matches (two, in this example) are replaced in $myGlobalString, because the g modifier is specified:

$myGlobalString =~ s/$myPattern/$myReplacementString/g;

Then the original string, the string after a single replacement, and the string after global replacement are displayed:

print “The original string was ‘$myTestString’.\n\n”;

print “After a single replacement it became ‘$myOnceString’.\n\n”; print “After global replacement it became ‘$myGlobalString’.\n\n”;

Using s/// with the Default Variable

The default variable, $_, can be used with s/// to search and replace the value held in the default variable.

Two forms of syntax can be used. You can use the normal s/// syntax, with the variable name, the =~ operator and the pattern and replacement text:

$_ =~ s/pattern/replacementText/modifiers;

The alternative, more succinct, syntax allows the name of the default variable and =~ operator to be omitted. So you can simply write the following:

s/pattern/replacementText/modifiers

Try It Out

Using s/// with the Default Variable

1.Type the following code in a text editor:

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

$_ = “I went to a training course from Star Training Company.”; print “The default string, \$_, contains ‘$_’.\n\n”;

if (s/Star/Moon/)

{

print “A replacement has taken place using the default variable.\n”;

print “The replaced string in \$_ is now ‘$_’.”;

}

2.

3.

Save the code as ReplaceDefaultVariable.pl.

Run the code, and inspect the displayed result, as shown in Figure 26-19.

681

Chapter 26

Figure 26-19

How It Works

The test string is assigned to the default variable, $_:

$_ = “I went to a training course from Star Training Company.”;

The value contained in the default variable is displayed:

print “The default string, \$_, contains ‘$_’.\n\n”;

The test of the if statement uses the abbreviated syntax for carrying out a replacement on the default variable:

if (s/Star/Moon/)

You might prefer to use the full syntax:

if ($_ =~ s/Star/Moon/)

Whichever syntax you use, the user is then informed that a replacement operation has taken place and is informed of the value of the string after the replacement operation:

print “A replacement has taken place using the default variable.\n”;

print “The replaced string in \$_ is now ‘$_’.”;

Using the split Operator

The split operator is used to split a test string according to the match for a regular expression.

The following example shows how you can separate a comma-separated sequence of values into its component parts.

Try It Out

Using the split Operator

1.Type the following code into a text editor:

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

my $myTestString = “A, B, C, D”;

print “The original string was ‘$myTestString’.\n”; my @myArray = split/,\s?/, $myTestString;

682

Regular Expressions in Perl

print “The string has been split into four array elements:\n”; print “$myArray[0]\n”;

print “$myArray[1]\n”; print “$myArray[2]\n”; print “$myArray[3]\n”;

print “Displaying array elements using the ‘foreach’ statement:\n”; foreach my $mySplit (split/,\s?/, $myTestString)

{

print “$mySplit\n”;

}

2.

3.

Save the code as SplitDemo.pl.

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

Figure 26-20

How It Works

A sequence of values separated by commas and a space character is assigned to the variable $myTestString:

my $myTestString = “A, B, C, D”;

The value of the original string is displayed:

print “The original string was ‘$myTestString’.\n”;

The @myArray array is assigned the result of using the split operator. The pattern that is matched against is a comma optionally followed by a whitespace character. The target of the split operator is the variable $myTestString:

my @myArray = split/,\s?/, $myTestString;

Then you can use array indices to display the components into which the string has been split:

print “The string has been split into four array elements:\n”; print “$myArray[0]\n”;

print “$myArray[1]\n”; print “$myArray[2]\n”; print “$myArray[3]\n”;

683

Chapter 26

Or, more elegantly, you can use a foreach statement to display each result of splitting the $myTestString variable:

print “Displaying array elements using the ‘foreach’ statement:\n”;

foreach my $mySplit (split/,\s?/, $myTestString)

{

print “$mySplit\n”;

}

The Metacharacters Suppor ted in Perl

Perl supports a useful range of metacharacters, as summarized in the following table.

Metacharacter

Description

 

 

. (period character)

Matches any character (with the exception, according to mode, of the new-

 

line character).

\w

Matches a character that is alphabetic, numeric, or an underscore character.

 

Sometimes called a “word character.” Equivalent to the character class

 

[A-Za-z0-9_].

\W

Matches a character that is not alphabetic, numeric, or an underscore char-

 

acter. Equivalent to the character class [^A-Za-z0-9_] or [^\w].

\s

Matches a whitespace character.

\S

Matches a character that is not a whitespace character.

\d

Matches a character that is a numeric digit. Equivalent to the character

 

class [0-9].

\D

Matches a character that is not a numeric digit. Equivalent to the character

 

class [^0-9].

?

Quantifier. Matches if the preceding character or group occurs zero or one

 

time.

*

Quantifier. Matches if the preceding character or group occurs zero or more

 

times.

+

Quantifier. Matches if the preceding character or group occurs one or more

 

times.

{n,m}

Quantifier. Matches if the preceding character or group occurs a minimum

 

of n times and a maximum of m times.

(...)

Capturing parentheses.

$1 etc

Variables that allow access to captured groups

|

Alternation character.

684