My regex is supposed to capture the names of all function declarations:
([\w{1}][\w_]+)(?=\(.+{)
In JavaScript it works as expected:
'int main() {\r\nfunctionCall();\r\nfunctionDeclaration() {}\r\n}'.match(/([\w{1}][\w_]+)(?=\(.+{)/g);
// [ 'main', 'functionDeclaration' ]
In C++ Builder I get this error:
regex_error(error_badrepeat): One of *?+{ was not preceded by a valid regular expression.
Minimal Reproducible Example:
#include <iostream>
#include <regex>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<string> matches;
string text = "int main() {\r\nfunctionCall();\r\nfunctionDeclaration() {}\r\n}";
try {
//regex myRegex("([\\w{1}][\\w_]+)(?=\\()"); works as intended
regex myRegex("([\\w{1}][\\w_]+)(?=\\(.+{)"); // throws error
sregex_iterator next(text.begin(), text.end(), myRegex);
sregex_iterator end;
while (next != end) {
smatch match = *next;
cout << match.str() << endl;
next++;
}
} catch (regex_error &e) {
cout << "([\\w{1}][\\w_]+)(?=\\(.+{)"
<< "\n"
<< e.what() << endl;
}
}
I used g++ to compile the above, instead of C++ Builder, and the error it gives is different: Unexpected character in brace expression.