Search code examples
c++regexc++11function-signature

c++ regex for fetching function signature


I am trying to search a function signature using regex_search in c++. Here's what I am using

std::smatch m;

std::regex e("?(unsigned|signed|const|inline)?\\s?(bool|char|int|float|double|void|wchar_t|string)\\s?[*]?(\\w.*)[(](.*)[)]\\s?{?$");

if(regex_search(xmlFileLine, m, e)) {

cout << xmlFileLine << endl;

}

In the above code 'xmlFileLine' is a string from getline reading each line of the input file. For instance, the string I am trying to match is "int Testo::Fact(int n) {"

When I execute the above regex in try and had all the regex_error flags like paren, complexity and so on... in catch, I got to know it was regex_paren thrown. I am not getting how to resolve it.

Please help me out in this.


Solution

  • A good place to test regular expressions is at regex101.com Placing your expression in there, it complains about the first ? with "Preceding token is not quantifiable". Once I remove that, it matches your test string.