Share via

XML Schema Regular Expressions

XML Schema regular expressions are used with the pattern facet to provide constraints or restrictions on a string of characters that conform to the defined pattern.

The pattern facet is a constraint on the value space of a data type, achieved by constraining the lexical space to literals that match a specific pattern. The value of pattern must be a regular expression.

Pattern provides for constraining a value space to values that are denoted by literals that match a specific regular expression.


The following example defines a user-derived data type that creates a representation of postal codes in the United States, limited to strings that match a specific regular expression.

Input file: T.xml

<zips xmlns:xsi="" xsi:noNamespaceSchemaLocation="t.xsd">

XML Schema: T.xsd

<?xml version="1.0"?>
<xs:schema xmlns:xs="" >
    <xs:element name="zips">
    <xs:sequence maxOccurs="unbounded">
      <xs:element name="zip" type="better-us-zipcode"></xs:element>
    <xs:simpleType name="better-us-zipcode">
        <xs:restriction base="xs:string">
            <xs:pattern value="[0-9]{5}(-[0-9]{4})?"/>

There are two types of characters that are used in regular expressions: metacharacters and normal characters. A metacharacter is one of the following characters: ., \, ?, *, +, {, }, (, ), [, or ]. These characters have special meaning in regular expressions, but can be escaped to from atoms that denote the sets of strings containing only themselves. An escaped metacharacter has the behavior of a normal character.

A normal character is any XML character that is not a metacharacter. In regular expressions, a normal character is an atom that denotes the singleton set of strings containing only itself.

The following topics describe the XML Schema regular expressions.

To understand the relationships in XML Schema Regular Expressions, see the XML Schema Regular Expressions Reference Chart.

See Also


XML Schemas (XSD) Reference
XML Schema Regular Expressions Reference Chart


Data Type Facets