mardi 6 avril 2021

Is it possible to use std::regex and std::match_results with custom string type?

I am working on a project that has a custom string type, MyString. I can match on it just fine like this:

MyString getMatch() {
  static const std::regex MyRegularExpression(R"RX(^blah=([a-z0-9]+)$)RX");
  const MyString str = "blah=25";
  std::cmatch match;
  if (std::regex_match(str.GetConstCharPtr(), match, MyRegularExpression))
  {
    return MyString(match[1].str().c_str());
  }
  else {
    return "";
  }
}

But as you can see, I have to convert from cmatchs std::string back to MyString when returning the value. It's not a big deal at all, but I noticed that std::match_results is heavily templated, so I was wondered if it's possible to somehow put MyString to its template arguments to get match results already as MyString.

Is it possible? If yes, how to do it?

Aucun commentaire:

Enregistrer un commentaire