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

Chapter 23

Figure 23-24

How It Works

First, a value that could be a U.S. Social Security number (SSN) is assigned to the variable $US_SSN:

$US_SSN = “123-12-1234”;

The preg_match() function is used to establish if there is a match against the pattern \d{3}-\d{2}- \d{4}. However, because the x matching modifier is used, you can spread the pattern over several lines, using the # character to enter comments about the meaning of the pattern’s individual components:

$myMatch = preg_match(“/

\d{3} # Matches three numeric digits

-# Matches a literal hyphen \d{2} # Matches two numeric digits

-# Matches a literal hyphen \d{4} # Matches four numeric digits

The x modifier is written after the second forward slash, which delimits the regular expression pattern. The value of the match is returned in the $theMatch array:

/x”, “123-12-1234”, $theMatch);

Then the user is informed of the result of the attempted matching:

echo “<p>The test string was: ‘$US_SSN’.</p>”;

echo “<p>This matches the pattern /\d{3}-\d{2}-\d{4}/</p>”;

Exercises

1.Are Perl Compatible Regular Expressions usable with all versions of PHP?

2.Which matching modifier is used to allow multiline comments inside a pattern?

590

24

Regular Expressions in

W3C XML Schema

Increasing volumes of data are being stored or transmitted as Extreme Markup Language (XML). Establishing whether that data is valid or not can be very useful. In addition, when an XML data structure holds data such as a credit card number, a Social Security number, or a postal code, it can be useful to apply regular expressions to establish whether the content of the XML data structure corresponds to the desired structure of the data. W3C XML Schema supports several constructs that provide support for controlling the content of parts of an XML document.

In this chapter, you will learn the following:

Basics of how W3C XML Schema works and how a W3C XML Schema document is associated with an XML instance document

How regular expressions and other constraints on content of an XML instance document are expressed

How Unicode affects how W3C XML Schema is used

What metacharacters are supported in W3C XML Schema

In this chapter, the term W3C XML Schema is used to refer to the XML schema definition language specified by the World Wide Web Consortium (W3C).

Details of the W3C XML Schema specifications and an introductory primer are located at www.w3.org/TR/xmlschema-0, www.w3.org/TR/xmlschema-1, and www.w3.org/TR/ xmlschema-2. Apart from the primer, you will likely find it useful to refer to books on W3C XML Schema such as XML Schema Essentials, by R. Allen Wyke and Andrew Watt (Wiley 2002).