Validating date in c

26 Apr

That makes it easy to change the format from mm/dd/yy to dd/mm/yy by changing which capturing group is treated as the month.When programming, make things easier with a bit of extra code.This will be particularly helpful if you want to add extra checks on the date later. Techniques used in the regular expressions in this recipe are discussed in Chapter 2. Any contributions and suggestions are most welcome! @adamwaite Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

Please ensure any new code is covered with unit tests, and that all existing tests pass. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.

IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

If the month has 30 days, groups 3 and 4 return the month and day. See the section Groups with the same name in Recipe 2.11 for details.

If the month has 31 days, groups 5 and 6 take action. We take advantage of this by using the same names “month” and “day” in each of the alternatives.

We used the first regex from Recipe 4.4 that allows any number between 0 and 39 for the day and month.