0

I want to print a value in boost::u32regex & reg using std::cout.

For boost::regex & reg, I can print reg.str() but not able to use str() to boost::u32regex.

Can anyone please tell me ?

Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
Rayappan A
  • 49
  • 1
  • 5
  • 2
    Do you have a little piece of code, with a working example with `boost::regex` and another one not working with `boost::u32regex`. And do you have any error message ? – Pierre Fourgeaud Jul 31 '13 at 10:46
  • const char* er = "(\d{1,2})\/(\d{1,2})\/(\d{2}|\d{4})"; boost::regex rNormal( er ); std::cout << "Normal regular expression :" << rNormal.str() << std::endl; boost::u32regex r = boost::make_u32regex("(?:\\A|.*\\\\)([^\\\\]+)"); std::cout << "u32 regular expression :" << r.str() << std::endl; – Rayappan A Jul 31 '13 at 13:45
  • 1
    You should edit you post with this sample. Did you read the answer I made ? – Pierre Fourgeaud Jul 31 '13 at 13:47

1 Answers1

2

It seems that the type used behind boost::u32regex is not compatible with cout. It seems they are using Uchar32 from the ICU library.

You can print your regex value by using iterators :

#include <boost/regex.hpp>
#include <boost/regex/icu.hpp>
#include <unicode/ustream.h>

void PrintRegex32( const boost::u32regex& r )
{
    boost::u32regex::iterator it  = r.begin();
    boost::u32regex::iterator ite = r.end();

    for ( ; it != ite; ++it )
    {
        std::cout << UnicodeString(*it) << std::endl;
    }
}

This is working for me. It is not as easy as printing a boost::regex value but it works. I suggest you to create a function to do so, like in the example.

EDIT :

You can try the code :

boost::u32regex r = boost::make_u32regex("(?:\\A|.*\\\)([^\\\]+)");
PrintRegex32( r );

I can print reg.str()

Just for the information, boost::basic_regex has an operator<< overload who are doing exactly the same thing so :

// reg is a boost::regex
std::cout << reg.str() << std::endl;

is the same thing as

// reg is a boost::regex
std::cout << reg << std::endl;
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
  • i am getting this error "template2.cc:11:29: error: invalid types âconst int[int]â for array subscript" – Rayappan A Jul 31 '13 at 14:00
  • my compilation statement is "g++ -o template2 template2.cc -lboost_regex -licuuc -std=gnu++0x". anything need to be linked? – Rayappan A Jul 31 '13 at 14:03
  • @PierreFourgeaud `UChar32` is a typedef of `int` on my (and, apparently, OP's) Linux, so `(*it)[0]` is an error. This needs ICU-compatible output. – Cubbi Jul 31 '13 at 14:25
  • @RayappanA If it is for debugging purpose, I edited my post with a solution. I just tried it and it works fine. – Pierre Fourgeaud Jul 31 '13 at 14:54
  • @PierreFourgeaud still in my case it == ite, do i need to set any flag in compilation statement? my compilation statement is "g++ -o template2 template2.cc -lboost_regex -licuuc -licuio -std=gnu++0x" and OS is 64 bit ubuntu server version. – Rayappan A Jul 31 '13 at 15:27
  • @RayappanA That's weird... For me it works like a charm... Just with your compilation line... But I'm not on a 64bit OS... Are you sure you do not enter in the loop ? – Pierre Fourgeaud Jul 31 '13 at 20:39