Reputation: 89
I am trying to make a regular expression for date format in XSD 1.0 pattern restriction.
I want to check if the date 'format' is invalid, NOT date value.
Date format I want to check are;
Year: YYYY or YY
Month: MM or M or MMM or MMMM
Day: D or DD
and has zero or unbounded of . / ; (just space) ! @
For example, below value is vaild;
MMM. D. YYYY
MM/DD/YYYY
DD/MM.YYYY
YYMM DD
below is not valid;
dd%mmYYYY
20140404
YYYY-MM-YYYY-DD
I made a regular expression but have a problem. It checks 'YYYY-MM-YYYY'
as valid (YYYY is repeated)
([!@#%_\-=`~,\.\/;: ]*(YYYY|YY|M|MM|MMM|MMMM|D|DD)[!@#%_\-=`~,\.\/;: ]*)+
How can I make the regular expression check if there is only one YYYY|YY|M|..D|DD?
XML schema 1.0 / Xercers-J
Upvotes: 1
Views: 1448
Reputation: 6721
OK, as much as it puts a bad taste in my mouth to even paste this into the answer box, I think this may be what you're looking for (your scroll bar is going to get a reasonable workout today)...
(([!@#%_\-=`~,\.\/;: ]*(YY(YY)?([!@#%_\-=`~,\.\/;: ]*(M{1,4}([!@#%_\-=`~,\.\/;: ]*(DD?)?)?)?)?))|([!@#%_\-=`~,\.\/;: ]*(YY(YY)?([!@#%_\-=`~,\.\/;: ]*(DD?([!@#%_\-=`~,\.\/;: ]*(M{1,4})?)?)?)?))|([!@#%_\-=`~,\.\/;: ]*(M{1,4}([!@#%_\-=`~,\.\/;: ]*(DD?([!@#%_\-=`~,\.\/;: ]*(YY(YY)?)?)?)?)?))|([!@#%_\-=`~,\.\/;: ]*(M{1,4}([!@#%_\-=`~,\.\/;: ]*(YY(YY)?([!@#%_\-=`~,\.\/;: ]*(DD?)?)?)?)?))|([!@#%_\-=`~,\.\/;: ]*(DD?([!@#%_\-=`~,\.\/;: ]*(YY(YY)?([!@#%_\-=`~,\.\/;: ]*(M{1,4})?)?)?)?))|([!@#%_\-=`~,\.\/;: ]*(DD?([!@#%_\-=`~,\.\/;: ]*(M{1,4}([!@#%_\-=`~,\.\/;: ]*(YY(YY)?)?)?)?)?)))[!@#%_\-=`~,\.\/;: ]*
Got all that? This is it broken down into the 9 possibilities of the order in which the token types (Year, Month or Day) can appear (if I understand you correctly)... I can explain if you ask, but don't wanna waste time if nobody is interested - even someone stumbling on this answer and commenting and asking for an explanation is enough - just let me know.
(
([!@#%_\-=`~,\.\/;: ]*(YY(YY)?([!@#%_\-=`~,\.\/;: ]*(M{1,4}([!@#%_\-=`~,\.\/;: ]*(DD?)?)?)?)?))|
([!@#%_\-=`~,\.\/;: ]*(YY(YY)?([!@#%_\-=`~,\.\/;: ]*(DD?([!@#%_\-=`~,\.\/;: ]*(M{1,4})?)?)?)?))|
([!@#%_\-=`~,\.\/;: ]*(M{1,4}([!@#%_\-=`~,\.\/;: ]*(DD?([!@#%_\-=`~,\.\/;: ]*(YY(YY)?)?)?)?)?))|
([!@#%_\-=`~,\.\/;: ]*(M{1,4}([!@#%_\-=`~,\.\/;: ]*(YY(YY)?([!@#%_\-=`~,\.\/;: ]*(DD?)?)?)?)?))|
([!@#%_\-=`~,\.\/;: ]*(DD?([!@#%_\-=`~,\.\/;: ]*(YY(YY)?([!@#%_\-=`~,\.\/;: ]*(M{1,4})?)?)?)?))|
([!@#%_\-=`~,\.\/;: ]*(DD?([!@#%_\-=`~,\.\/;: ]*(M{1,4}([!@#%_\-=`~,\.\/;: ]*(YY(YY)?)?)?)?)?))
)[!@#%_\-=`~,\.\/;: ]*
Upvotes: 1