Re: [Boost-users] [date_time] How to create a date from stringwithspecific format?

-----Original Message----- From: Christian Henning [mailto:chhenning@gmail.com] Sent: Wednesday, March 22, 2006 9:22 AM To: boost-users@lists.boost.org Subject: Re: [Boost-users] [date_time] How to create a date from stringwithspecific format?
Andrew, thanks for pointing out the submatch feature. Unfortunately no match can be found when I try your code. Is it working on your machine? Here, the dateMatch container size just 1.
Any ideas why?
Christian
The code seems to work correctly on my machine (MSVC8 with Boost 1.33.1). It throws an out of range error because strDate specifies November 33. If I change strDate to 11-30-1997, then I can "cout << d" and get the date.

Andrew, the following code is not working on my machine. Can you check if it is on your machine, please? The size of the match container is just one. I'm using VC7.1 and Boost 1.33.1. int _tmain(int argc, _TCHAR* argv[]) { //second approach boost::regex oDateReg2( "\\d{2}-\\d{2}-\\d{4}" ); std::string strDate2( "11-22-1997" ); boost::smatch dateMatch; if( boost::regex_match( strDate2 , dateMatch , oDateReg2 )) { try { size_t nMatchSize = dateMatch.size(); if( nMatchSize >= 4 ) { std::string strMonth = dateMatch[1]; std::string strDay = dateMatch[2]; std::string strYear = dateMatch[3]; unsigned int nYear = boost::lexical_cast<unsigned int>( strYear ); unsigned int nMonth = boost::lexical_cast<unsigned int>( strMonth ); unsigned int nDay = boost::lexical_cast<unsigned int>( strDay ); date d( nYear, nMonth, nDay ); date::ymd_type ymd = d.year_month_day(); } else { std::cerr << "Error. "; } } catch( std::out_of_range oEx ) { std::cout << "Error: " << oEx.what() << std::endl; } catch( std::exception oEx ) { std::cout << "Error: " << oEx.what() << std::endl; } } return 0; } When I run the captures_examples of the regex lib the smatch stuff works. But not here. Very strange. Thanks, Christian

Christian Henning wrote:
Andrew, the following code is not working on my machine. Can you check if it is on your machine, please? The size of the match container is just one. I'm using VC7.1 and Boost 1.33.1.
int _tmain(int argc, _TCHAR* argv[]) { //second approach boost::regex oDateReg2( "\\d{2}-\\d{2}-\\d{4}" ); std::string strDate2( "11-22-1997" );
This regular expression does not contain captures. The size of the match container is supposed to be 1. Try this instead: boost::regex oDateReg2( "(\\d{2})-(\\d{2})-(\\d{4})" ); Sebastian Redl
participants (3)
-
Andrew Holden
-
Christian Henning
-
Sebastian Redl