I have a function in c++ that takes in an input string representing a date of the format MM/DD/YYYY. The function uses the C implementation of regex due to limitations of my environment. I am attempting to extract the year, month, and date from the string.
#include <stdarg.h>
#include <string.h>
#include <iostream>
#include <regex.h>
#include <sys/types.h>
using namespace std;
void convertDate(string input)
{
char pattern[100];
regex_t preg[1];
regmatch_t match[100];
const char * reg_data = input.c_str();
string year;
string month;
string day;
strcpy(pattern, "^([0-9]{1,2})/([0-9]{1,2})/([0-9]{4})$");
int rc = regcomp(preg, pattern, REG_EXTENDED);
rc=regexec(preg, reg_data, 100, match, 0);
if( rc != REG_NOMATCH )
{
year = input.substr(match[3].rm_so, match[3].rm_eo);
month = input.substr(match[1].rm_so, match[1].rm_eo);
day = input.substr(match[2].rm_so, match[2].rm_eo);
cout << year << endl;
cout << month << endl;
cout << day << endl;
}
}
Here are some examples of input/output:
1) string input2 = "8/11/2014";
convertDate(input2);
2014
8
11/2
2) string input2 = "11/8/2014";
convertDate(input2);
2014
11
8/20
3) string input2 = "1/1/2014";
convertDate(input2);
2014
1
1/2
I'm not sure why the day is capturing a regex group of length 4, when the capture group states it should only be capturing 1 or 2 characters that are digits. Also, why would the day be having this issue, when the month is correct? They use the same logic, it looks like.
I used the documentation here