开发者

Regex to match function return types in vim

I'm using c++ with a header file that is quite large and would like to do a search for functions with specific return types, such as int. The problem lies in the fact that there is white space in front of the return types as shown below.

    Set<Animal*> _animals;

    int _getMonkeyCount(Zoo* zoo);

    bool _ad开发者_StackOverflow中文版dUnicorn(Animal* unicorn, int age = EXTINCT );

    string _toString(Zoo* z);

    void _addMonkeyz(Animal* monkey, List<string> fleaNames);

I would like to have a regex that matches one of the return types. For example bool, but I do not want to match the space in front of it.


You probably want something like:

/^\s\+\zsbool

The key here is \zs which defines the start of the match. There is also \ze to define the end of the match but you don't need that here.


How about

^\s\+int
^\s\+bool

etc.


'^ *int '

Remove the quotes when you run it. Of course, this highlights a few extra characters too...


Not sure if this is exactly what you're looking for, but this might help someone else (matches function definitions, probably can be improved).

This Perl regex supports:

  • pointers
  • names
  • function arguments
  • the static qualifier
  • the const qualifier (in both locations)
  • constructor parameters

/((?:static\s+)?(?:const\s+)?(?:\w+\s+)(?:\*\s+)?(?:const(:?\s*))?)([\w|~]+)(\(.*\))(.*)\{/

Issues:

  • no preprocessor support (e.g. dllexport, etc.)
  • extra match group seems to have snuck in
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜