SomethingElse
SomethingElse

Reputation: 327

Proper syntax of regex in XML Schema

I have regex for validating mobile number. It is working in PHP and JS, however when I try to implement it in xml schema I get error "Request XSD validation against my.xsd failed. "failed to compile: xmlFAParseAtom: expecting ')'","failed to compile: xmlFAParseRegExp: extra characters","Element '{http://www.w3.org/2001/XMLSchema}pattern': The value of the facet 'pattern' is not a valid regular expression."

Example of my XSD

<xs:simpleType name="mobile-number">
    <xs:restriction base="xs:string">
        <xs:pattern value="(?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?(?:\(?0\)?[\s-]?)?)|(?:\(?0))(?:(?:\d{5}\)?[\s-]?\d{4,5})|(?:\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3}))|(?:\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4})|(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}))(?:[\s-]?(?:x|ext\.?|\#)\d{3,4})?"/>
    </xs:restriction>
</xs:simpleType>

I tried only this part too (?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?(?:\(?0\)?[\s-]?)?)|(?:\(?0)) and got same error

Upvotes: 2

Views: 985

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626806

There are 14 groups in the pattern and all of them are non-capturing. XSD Schema regex syntax does not support such a grouping construct, you need to convert all non-capturing groups ((?:...)) to capturing ((...)) ones.

Use

<xs:pattern value="((\(?(0(0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?(\(?0\)?[\s-]?)?)|(\(?0))((\d{5}\)?[\s-]?\d{4,5})|(\d{4}\)?[\s-]?(\d{5}|\d{3}[\s-]?\d{3}))|(\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4})|(\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}))([\s-]?(x|ext\.?|#)\d{3,4})?" />

Upvotes: 2

Related Questions