Boost.regex with icu support using named capture groups

453 views Asked by At

Below test program uses the named captures support in boost-regex to extract year, month and day fields from a date (just to illustrate the use of named captures):

#include <boost/regex.hpp>
#include <boost/regex/icu.hpp>

#include <string>
#include <iostream>

int main(int argc, const char** argv)
{
   std::string   str = "2013-08-15";
   boost::regex  rex("(?<year>[0-9]{4}).*(?<month>[0-9]{2}).*(?<day>[0-9]{2})");
   boost::smatch res;

   std::string::const_iterator begin = str.begin();
   std::string::const_iterator end   = str.end();

   if (boost::regex_search(begin, end, res, rex))
   {
      std::cout << "Day:   " << res ["day"] << std::endl
                << "Month: " << res ["month"] << std::endl
                << "Year:  " << res ["year"] << std::endl;

   }
}

Compiled with

g++ regex.cpp -lboost_regex -lboost_locale -licuuc

This little program will produce the following output as expected:

$ ./a.out 
Day:   15
Month: 08
Year:  2013

Next I replace the ordinary regex parts with their u32regex counterparts:

#include <boost/regex.hpp>
#include <boost/regex/icu.hpp>

#include <string>
#include <iostream>

int main(int argc, const char** argv)
{
   std::string   str = "2013-08-15";
   boost::u32regex  rex = boost::make_u32regex("(?<year>[0-9]{4}).*(?<month>[0-9]{2}).*(?<day>[0-9]{2})", boost::regex_constants::perl);
   boost::smatch res;

   std::string::const_iterator begin = str.begin();
   std::string::const_iterator end   = str.end();

   if (boost::u32regex_search(begin, end, res, rex))
   {
      std::cout << "Day:   " << res ["day"] << std::endl
                << "Month: " << res ["month"] << std::endl
                << "Year:  " << res ["year"] << std::endl;

   }
}

Building an running the program now results in a run-time exception suggesting an uninitialized shared_ptr:

$ ./a.out 
a.out: /usr/include/boost/smart_ptr/shared_ptr.hpp:648: typename boost::detail::sp_member_access<T>::type boost::shared_ptr<T>::operator->() const [with T = boost::re_detail::named_subexpressions; typename boost::detail::sp_member_access<T>::type = boost::re_detail::named_subexpressions*]: Assertion `px != 0' failed.

I'm not using shared pointers directly though.

This is with boost 1.58.1 and gcc 5.3.1.

How can I get the u32regex version of the program running correctly as well ?

0

There are 0 answers