I am trying to do regex_match on a string which have square brackets([...]
) inside it.
Things I have tried so far:
- Normal matching
- Backslashing the square brackets with 1 slash
- Backslashing the square brackets with 2 slashes
Code to repro:
#include <iostream>
#include <cstring>
#include <regex>
using namespace std;
int main () {
std::string str1 = "a/b/c[2]/d";
std::string str2 = "(.*)a/b/c[2]/d(.*)";
std::regex e(str2);
std::cout << "str1 = " << str1 << std::endl;
std::cout << "str2 = " << str2 << std::endl;
if (regex_match(str1, e)) {
std::cout << "matched" << std::endl;
}
}
This is the error message I get every time I compile it.
terminate called after throwing an instance of 'std::regex_error'
what(): regex_error
Aborted (core dumped)
I was told by stack overflow members that gcc 4.8 or earlier version of it are known to be buggy. So, I needed to update it to latest version.
I have created an Ideone fiddle where compiler should not be issue. Even there, I do not see regex_match happening.