Help with regular expression - DateTime validation in XSD
I have the following:
<xsd:restriction base="xsd:dateTime">
<xsd:pattern value="[1-9][0-9]{3}\-.+T[^\.]+(Z|[\+\-].+)"/>
</xsd:restriction>
I then get an error when I use something like开发者_开发技巧 this: 2011-06-167T09:30:47.0Z
or this:2011-06-16T09:30:47.0Z
Could you please help me figure out what is wrong with the datetime I am validating?
I do not control the XSD with the RegEx - all I can do is to make my dates comply with it.Thanks!
[1-9][0-9]{3}-.+T[^.]+(Z|[+-].+)
(unnecessary escapes removed) fails because your test string doesn't fulfill the rules of the regex which are:
[1-9][0-9]{3} # match a four-digit number > 999 : 2011
- # dash : -
.+ # one or more unspecified characters : 06-167
T # a T : T
[^.]+ # one or more characters except dot : 09:30:47
( # followed by either :
Z # a Z : ???
| # or :
[+-] # a plus or minus sign : ???
.+ # and one or more unspecified characters :
)
Your test string 2011-06-167T09:30:47.0Z
does contain a dot before the Z
(and there is no plus/minus sign that would allow the alternative to match) so the regex fails.
The question is: What are the rules you do need to follow? How are datetimes defined in your application?
You could try a non greedy matching, perhaps that solves it:
<xsd:pattern value="[1-9][0-9]{3}\-.+?T[^\.]+?(Z|[\+\-].+)"/>
With the ? after a + it searches for the smallest possible not the longest possible part of .
Futhermore I'mk not sure if that works, as you want it to:
[^\.]
Try without the ^
精彩评论