Reputation: 2773
I am working on a regular expression that should allow a format of yyyymmdd-nn. (4 digit year between 2010 and 2059, 2 digit month, 2 digit day, a dash and then a value between 01 and 99).
This works ...(20[1-5]\d(0|1)\d[0-3]\d-\d[1-9])
The second part is that I also need to allow an empty value. Ideally I would like it to be null but it is okay if that is not possible and the user enters up to 11 spaces.
I've tried different variations of using the \s
(\s{0-11})|(20[1-5]\d(0|1)\d[0-3]\d-\d[1-9])
(\s?)|(20[1-5]\d(0|1)\d[0-3]\d-\d[1-9])
(\s*)(20[1-5]\d(0|1)\d[0-3]\d-\d[1-9])
.
These all tell me it there are an infinite number of possibilities.
I have also tried...
(20[1-5]\d(0|1)\d[0-3]\d-\d[1-9])|null
(20[1-5]\d(0|1)\d[0-3]\d-\d[1-9])?
^(20[1-5]\d(0|1)\d[0-3]\d-\d[1-9])?$
None of these seem to allow an empty value either.
Can someone tell me what I need to add to my regular expression to allow the value to be empty?
Upvotes: 0
Views: 84
Reputation: 8332
You're really close. What you need is to match your RE, or an empty string. Thus
^(20[1-5]\d(0|1)\d[0-3]\d-\d[1-9]|)$
should do the trick. (N.b. the |
ending the capture group.)
Regards.
Upvotes: 2