I’m trying to match a C/C++ function definition using a fairly complex regular expression. I’ve found a case where it’s not working and I’m trying to understand why. Here is the input string which does not match:
void Dump(const char * itemName, ofstream & os)
which clearly is a valid C++ method declaration. Here is the RE:
^[^=+-|#]*?([\w<>]+\s+(?!if|for|switch|while|catch|return)\w+)\s*\([^;=+-|]*$
This basically tries to distinguish between other C syntax which looks like a method declaration, i.e. which has words followed by paraentheses.
Using the very useful Python regular expression debugger (http://www.pythonregex.com/) I’ve narrowed it down to the trailing “$” – if I remove the trailing $ in the regular expression, it matches the method signature above; if I leave in the $, it doesn’t. There must be some idiosyncracy of Python RE’s that is eluding me here. Thanks.
The use of
+-|in your character class[^;=+-|]is a range specification. This will result in the character class containing (actually excluding since you’re using^) much more than you intend. To specify a literal-in a character class, mention it first like[^-;=+|].