1

I'm working on a simple NLP project which given a string, the various parameters would be determined.

Given the following input:

07122012 12102012

Code:

string REGEX_DATE = "((\\d{2})/(\\d{2})/(\\d{4}))";

regex expressionFormat(REGEX_DATE);

sregex_token_iterator i(input.begin(), input.end(), expressionFormat, 1);

sregex_token_iterator j;

while(i != j)
{
result = *i++;
}

What would be the best way to store and compare the results? (Determine which date is earlier)

Larry Lee
  • 61
  • 1
  • 6

1 Answers1

1

The best way would be to construct and compare the dates rather than strings or numbers:

#include <iostream>
#include <string>
#include <boost/date_time.hpp>

int main()
{
    std::string input = "07122012 12102012";

    std::istringstream buf(input);
    buf.imbue(std::locale(buf.getloc(),
              new boost::posix_time::time_input_facet("%d%m%Y")));

    boost::posix_time::ptime d1, d2;
    buf >> d1 >> d2;

    if(d1 < d2)
        std::cout << d1 << " before " << d2 << '\n';
    else
        std::cout << d2 << " before " << d1 << '\n';
}

online demo: http://liveworkspace.org/code/989ba879e622aed7866e7dba2d0f02ee

Cubbi
  • 46,567
  • 13
  • 103
  • 169
  • I'm actually using boost's regex tokenizer to parse in the inputs if you're wondering why i asked that – Larry Lee Oct 16 '12 at 02:52
  • @Larry Lee `buf >> d1 >> d2` is parsing the string containing whitespace-separated dates in the DDMMYYYY format, and storing the results in `d1` and `d2`. You could certainly parse it with regex and convert the individual regex matches to date/time objects for comparison, either the same way, or using the various constructors of the boost's date and time classes, this is just a step shorter. – Cubbi Oct 16 '12 at 02:59