0

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.

GirkovArpa
  • 4,427
  • 4
  • 14
  • 43
  • Do you use Raw string? else you have to escape ```\``` manually – Jarod42 May 04 '20 at 06:36
  • 1
    Can you post the `C++` code that produces the error? – Galik May 04 '20 at 06:36
  • 1
    I guess you forgot to convert `\ ` to `\\ `. Please post a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – MikeCAT May 04 '20 at 06:37
  • BTW, your regex match `if(condition)` as function, which is wrong, and misses some functions too (space between name and parenthesis). name can also digits (as non-first position) – Jarod42 May 04 '20 at 06:44
  • Maybe [this answer](https://stackoverflow.com/a/35914783/2064981) or another of the answers in the question will help you. – SamWhan May 04 '20 at 07:09
  • @MikeCAT I added a minimal reproducible example. I hope it helps. – GirkovArpa May 04 '20 at 07:10

1 Answers1

0

The correct regex string literal for C++ is this:

"([\\w{1}][\\w_]+)(?=\\(.+\\{)"

The { must be escaped, unlike in JavaScript.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
GirkovArpa
  • 4,427
  • 4
  • 14
  • 43
  • 1
    Or, using a C++11 style [raw string literal](https://en.cppreference.com/w/cpp/language/string_literal), which does not require escaping \ characters: `R"(([\w{1}][\w_]+)(?=\(.+\{))"` – Remy Lebeau May 09 '20 at 00:45