Tamás Szelei
Tamás Szelei

Reputation: 23971

Regex to parse C++ function return type (for a practical subset of C++)

Consider a declaration like this:

virtual Foo * const operator=(Bar& b);

or

virtual Foo * const operator=   (Bar& b); // note the optional number of whitespace after the function name

I want to capture the Foo * const part. In other words, I would like to match all characters between the start of the string, an optional virtual|static and an opening brace, except for the last word and an optional amount of whitespace before the brace.

I came up with the following:

(?virtual|static)?\s*(?.*?)(?!\s*\()

But this does not work.

Edit.: some more examples:

Input                             Capture
-----                             -------
void f();                         void
static int Foo();                 int
virtual const int * const         const int * const
virtual Foo * operator+(Foo& b);  Foo *

Upvotes: 1

Views: 179

Answers (1)

Jerry
Jerry

Reputation: 71598

I'm not too sure what you are looking for, but if I got it right, this is probably what you are looking for:

^(?:virtual|static)?\s*(.*?)(?=\s\w+=)

And tested on here.

The lookahead is not necessary though:

^(?:virtual|static)?\s*(.*?)\s\w+=

Otherwise, if you want to get the whole Foo * const operator= ( part, maybe this?

^(?:virtual|static)?\s*(.*?)\(

I feel like you don't understand the negative lookahead or non-capturing group well.

(?:...) is a non capturing group (notice the colons).

(?!...) is a negative lookahead, which prevents matching of a character being followed by what's in the group. You would rather prefer a positive lookahead in your case to capture everything before that bracket, but it's not necessary in this particular case since a simple .*\( will suffice.

EDIT: Subject to new test samples, a bit more complicated regex:

^(?:virtual|static)?\s*(.+?)(?:(?:\s*\w+[\(=\+\?\!])|$)

Upvotes: 1

Related Questions