Glib::Regex extracting values



Hi all,

I am currently converting the std::string objects in my gtkmm project
to Glib::ustring.  My program makes use of regex and I was using the
pcrecpp library but am now moving over to Glib::Regex for
compatibility with Glib::ustring.  So far I have learned that I can
replace part of a string as follows:

Glib::ustring default_style = "DAY MONTH YEAR";
Glib::RefPtr<Glib::Regex> my_regex = Glib::Regex::create("MONTH");
Glib::ustring replacement = "10";
Glib::ustring month = my_regex->replace_literal(default_style, 1,
replacement, Glib::REGEX_MATCH_PARTIAL);
std::cout << month;

and I can extract part of a string as follows:

    std::vector<Glib::ustring> string_list;
    try{
      Glib::ustring match_string;
      string_list = Glib::Regex::split_simple("(\\d+)", month);
    }
    catch(Glib::RegexError err){
      std::cerr << err.code() << "\n";
    }
    std::cout << "Length of string_list: " << string_list.size() << "\n";
    for(std::vector<Glib::ustring>::iterator i = string_list.begin();
	i != string_list.end(); i++){
      std::cout << "string_list: " << *i << "\n";
    }
    Glib::ustring month_string = string_list.at(1);
    std::cout << "month_string is: " << month_string << "\n";


but this seems like a lot of work as compared with pcrecpp where I can
simply use:

int month_int;
pcrecpp::RE("(\\d+)>").PartialMatch(month, &month_int);

Especially as I get an integer value with pcrecpp, but with
Glib::Regex the captured text is a Glib::ustring and I then have to
convert this to an int.

So, is there something I am missing with Glib::Regex.  Any code
examples someone would be willing to share?

Thanks, Jason


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]