Below is the C++ program which uses regex to match a string, but is not working. My OS is Ubuntu Linux and compiler is standard C++ compiler which comes with ubuntu.
#include <iostream>
#include <regex>
using namespace std;
int main() {
const char s[] = "This is a target string";
regex e ("^([0-9a-zA-Z]*).*?");
cmatch matches;
if(regex_match(s, matches, e)) {
cout << "Match found" << endl;
for (int i = 0; i < matches.size(); ++i) {
cout << matches[i] << endl;
}
}
return 0;
}
On compiling using g++ like below
g++ -o test test.cpp -std=c++11
And running the program is failing with the below output
terminate called after throwing an instance of 'std::regex_error'
what(): regex_error
[1] 24898 abort (core dumped) ./test
The pattern is working fine and I tried it in rubular.com
.I am expecting it to print
Match Found
This is a target string
This
I am new to using regular expressions in C++. Please point me in the right direction.