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

PHP and Regular Expressions

2.Save the code as C:\inetpub\wwwroot\PHP\EregSpaceTest.php.

3.Enter the URL http://localhost/PHP/EregSpaceTest.php in Internet Explorer, and inspect the results, as shown in Figure 23-22.

Figure 23-22

How It Works

The ereg() function’s first argument matches a literal o followed by a space character, followed by a literal w. In the test string Hello world!, the matched text is the final o of Hello followed by a space character, followed by the initial w of world:

$match = ereg(‘o[[:space:]]+w’, “Hello world!”, $matches);

The user is informed that a match exists, and the zeroth element of the $matches array is used to display the matched value to the user:

echo “<p>A match was found.</p>”;

echo “<p>$matches[0]</p>”;

Supported Metacharacters with PCRE

The following table summarizes the metacharacters that are supported with PCRE. If you compare this with the table showing the metacharacters supported in the ereg() family of functions, you will see that there is much more functionality in PCRE.

Metacharacter

Description

 

 

^

Matches the position at the beginning of a string (or line in multiline

 

mode).

$

Matches the position at the end of a string (or line in multiline mode).

. (period character)

Matches any character except newline (by default).

[...]

Character class. Matches any character contained inside the square

 

brackets once.

 

Table continued on following page

585

Chapter 23

Metacharacter

Description

 

 

[^...]

Negated character class. Matches any character once except those

 

contained inside the square brackets.

|

Alternation.

()

Groups the character sequence matching the pattern inside the

 

paired parentheses.

?

Quantifier. Matches zero or one occurrence of the preceding character

 

or group.

*

Quantifier. Matches zero or more occurrences of the preceding char-

 

acter or group.

+

Quantifier. Matches one or more occurrences of the preceding charac-

 

ter or group.

{n,m}

Quantifier. Matches at least n occurrences and at most m occurrences

 

of the preceding character or group.

\d

Matches a numeric digit.

\D

Matches any character except a numeric digit.

\s

Matches a whitespace character.

\S

Matches any character except a whitespace character.

\w

Matches any Perl “word” character. Equivalent to the character class

 

[A-Za-z0-9_].

\W

Matches any character except a Perl “word” character. Equivalent to

 

the negated character class [^A-Za-z0-9_].

\b

Matches a position between a \w character and a \W character.

\B

Matches a position that is not between a \w character and a \W character.

\A

Matches the position at the beginning of a string. Its operation is not

 

affected by multiline mode.

\z

Matches the position at the end of a string. Its operation is not

 

affected by multiline mode.

 

 

Positional Metacharacters

The ^ and $ metacharacters allow matching of the position at the beginning of a test string and the end of a test string, respectively. If the m matching modifier is used, the ^ metacharacter matches the position at the beginning of a line, and the $ metacharacter matches the position at the end of a line.

586

PHP and Regular Expressions

Character Classes in PHP

The PCRE functionality in PHP supports a full range of character class functionality, including ranges and negated character classes.

The following example illustrates how a negated character class can be used to test that only a desired type of character is present in a string.

Try It Out

Using a Negated Character Class

1.Enter the following code in a text editor:

<html>

<head>

<title>Negated Character Class Example</title> </head>

<body>

<?php

$sequenceToMatch1 = “12345”; $sequenceToMatch2 = “123 45”; $negCharClass = “/[^0-9]/”;

$nonNumMatch = preg_match($negCharClass, $sequenceToMatch1); if ($nonNumMatch)

{

echo “<p>There was a non-numeric character in $sequenceToMatch1.</p>”;

}

else

{

echo “<p>All characters were numeric in $sequenceToMatch1.</p>”;

}

$nonNumMatch = preg_match($negCharClass, $sequenceToMatch2); if ($nonNumMatch)

{

echo “<p>A non-numeric character was found in $sequenceToMatch2.</p>”;

}

else

{

echo “<p>All characters were numeric in $sequenceToMatch2.</p>”;

}

?>

</body>

</html>

2.Save the code as C:\inetpub\wwwroot\PHP\NegatedCharacterClass.php.

3.In Internet Explorer, enter the URL http://localhost/PHP/NegatedCharacterClass.php, and inspect the results displayed, as shown in Figure 23-23.

587

Chapter 23

Figure 23-23

How It Works

This example tests two strings to see if any undesired character is present in the string — in this case, a character that is not a numeric digit.

First, the two test strings are assigned to the variables $sequenceToMatch1 and $sequenceToMatch2. As you can see, $sequenceToMatch2 contains a space character, which is not a numeric digit. The $sequenceToMatch1 variable contains only numeric digits:

$sequenceToMatch1 = “12345”;

$sequenceToMatch2 = “123 45”;

The regular expression pattern is assigned to the $negCharClass variable and consists of the negated character class [^0-9], which will match any character that is not a numeric digit:

$negCharClass = “/[^0-9]/”;

The preg_match() function is used to test whether there is a character that is not a numeric digit in the $sequenceToMatch1 variable. Because the non-numeric character is not matched, the else clause is executed:

$nonNumMatch = preg_match($negCharClass, $sequenceToMatch1); if ($nonNumMatch)

{

echo “<p>There was a non-numeric character in $sequenceToMatch1.</p>”;

}

else

{

echo “<p>All characters were numeric in $sequenceToMatch1.</p>”;

}

Next, the preg_match() function is used to test $sequenceToMatch2. In this case, there is a nonnumeric character, so $nonNumMatch contains a match:

$nonNumMatch = preg_match($negCharClass, $sequenceToMatch2);

588