Q:

Regular expression pattern for allowing null and empty

Iam using below regular expression currently in XSD as below
<xs:pattern value="[A-Z]{3}"/>

This is working fine for alphabets.
But iam trying to allow null also to this , but could not
i tried putting 
[null]|[A-Z]{3}

but this is also not accepting null.
I want null or empty to be accepted.
please help

A:

A regex operates on text and cannot determine if  a string is null, it can only determine if a string is empty.
To make a match optional, you can enclose the whole pattern into a (non-)capturing group and add a ? quantifier after it:
<xs:pattern value="([A-Z]{3})?"/>

Since the pattern regex is anchored by default, this ([A-Z]{3})? will match either an empty string or a string that only has 3 uppercase ASCII letters.

