FYI, they're called
regular expressions, not real expressions. I don't think they're available in standard C, but there is a POSIX extension supporting them and there are 3rd party libraries that provide regex functions.
There are several other options as well:
1. If available, use
strptime.
2. Split the string using strtok. Use
strtol or strtoul to convert to int/long, it has better error detection (atoi has none). You can use the endptr parameter to check if there is garbage at the end of datestr.
3. Use sscanf to parse datestr and extract the year, month and day. You will need to make sure there is no garbage at the end of your datestr.
If you go for option 2 or 3, you need to remember that not all months have 31 days, some have 30 and February has 28 (29 in a leap year). A switch statement here is good for checking max day based on the month, especially since you can take advantage of fall-through (see below). Google will turn up formulas for determining leap year, to help you figure out if February should have 28 or 29 days for the given year.
Code:
switch (month) {
case 1: // fall-through
case 3: // fall-through
case 5: // fall-through
...
case 12:
max_days = 31;
break;
case 4: // fall-through
case 6: // fall-through
...
}
if (day > max_days)
printf("Bad date, month %d has %d days", month, max_days); // usually a good idea to tell the user why their input was invalid
It's a good idea to leave a comment stating that you explicitly want the cases to fall through, just to be clear you didn't forget a break;