0

Hi I am implementing regex using C++ .

Background:

I have a std::string and a std::regex. I need to compare the string against this regex . The regex used here is not about validation . My typical regex would be something like

a[bc]{2} and nothing beyond this scope

I have to pass this regex as a char pointer argument to a function .

Problem: I am unable to assign char pointer to std::regex. If I do so I am getting the following error.

terminate called after throwing an instance of std::regex_error what(): regex_error

My function body will be

std::string s((char*)a);  // The main string
std::regex e((char*)b); // Regex comparing the main string. a and b are the parameters to the function

if (std::regex_match(s, e))
{
    // returns the matched portion of the string 
    //  for instance  "abcdeef"  , "e{2}" would return ee
}
else 
{
      // return "Mismatch"
}

Any suggestions..? Or is there a way to extract the string from regex like "a{2}b" -> "aab" Thanks in advance

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
vikram
  • 69
  • 6

1 Answers1

0

The error is probably raised due to the ECMAScript syntax for the expression which doesn't support [] in regex. You can try with using the basic regex constrain tag.

std::regex e((char*)b, std::regex_constants::basic);

They are discussing more about this here.

Community
  • 1
  • 1
Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85