-1

I have a C++ regex to search for characters inside square brackets, for instance if the string is x[7:0], I want to return 7:0. This is what my regex looks like -

std::regex reg("\[(.*?)\]");

When I compile (g++) I get the following warning -

../fixedPointFormatter.cc:30:18: warning: unknown escape sequence: ']' std::regex reg("[(.*?)]"); ^~~~~~~~~~~

The following returns nothing...

if (regex_search(arg, matches, reg)) {
 for (int i=1; i<matches.size(); i++) {
   cout << matches[i] << endl;
 }
}

Can someone help identify what is wrong with this?

Rajat Mitra
  • 167
  • 1
  • 11

1 Answers1

0

The regex needs the [ and the ] to be escaped using \. However, to provide that to the regex, you need to escape the \s in the string.

std::regex reg("\\[(.*?)\\]");
R Sahu
  • 204,454
  • 14
  • 159
  • 270