0

I am trying to store a regular expression within a variable, i.e if we had a regular expression, \\d and a string, std::string str; then I would store the regular expression \\d within std::string str. From that I could then use str whenever I wanted to use that regular expression.

I tried something like this:

Boost::regex const string_matcher("\\d");
std::string str = string_matcher;

However I realized that it would not work. Does anyone have any ides of how I can store a regular expression?

Cœur
  • 37,241
  • 25
  • 195
  • 267
CodersSC
  • 710
  • 2
  • 13
  • 29
  • Sorry, I don't get the question. What's wrong with `std::string str = "\\d"; Boost::regex string_matcher(str);` – Niklas B. Jan 02 '12 at 16:37
  • 6
    You really need [a good introductory C++ book](http://stackoverflow.com/q/388242/46642). *You have the solution to your own problem in the code you posted*. – R. Martinho Fernandes Jan 02 '12 at 16:38

2 Answers2

3
std::string regex = "\\d";
boost::regex expression(regex);
bool ok = boost::regex_match(testStr, expression);
juergen d
  • 201,996
  • 37
  • 293
  • 362
3

You already have your regular expression stored in a variable. You called it string_matcher.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510