3

I have to extract a function name from various c++ function calls. Following are some of the function calls examples and extracted function names highlighted.

  • std::basic_fstream<char,std::char_traits<char> >::~basic_fstream<char,std::char_traits<char> > ~basic_fstream
  • CSocket::Send send
  • CMap<unsigned int,unsigned int &,tagLAUNCHOBJECT,tagLAUNCHOBJECT &>::RemoveAll Cerner::Foundations::String::Rep::~Rep~Rep

  • CCMessage::~CCMessage ~CCMessage

  • std::_Tree<std::_Tmap_traits<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,u_Tree
  • Lib::DispatcherCache::~DispatcherCache~DispatcherCache
  • CPrefDataObjectLoader<CPrefManagerKey,CPrefManagerValue,CGetPrefManager,PrefManagerKeyFunctor>::Get Get

    The following Regex works for most of the functions

  • /((?:[^:]*))$';/ This regex get the string from the last :
  • /+?(?=<)';/ This one removes string that starts with <

But for std::basic_fstream<char,std::char_traits<char> >::~basic_fstream<char,std::char_traits<char> > the output I get is char_traits because this string is after last ':' but the result should be ~basic_fstream. Is there a way I can combine both regex and ignore everything that is within <>?

SS Sid
  • 421
  • 6
  • 12
  • take your pick of online tools to help you get there: https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=online%20regex%20builder – Richard Hodges Feb 24 '16 at 18:59
  • @KenWite looks like that one asking to parse out function name from the source code. Here my question is more complex. I need to parse out function name from the different function calls. – SS Sid Feb 24 '16 at 19:04

1 Answers1

3

The grammar of C++ is not only not regular, it's actually highly context-sensitive (especially near templates). Even a proper CFG parser won't help you, let alone a plain old regex… Rather than trying to approximate the impossible using ugly and fragile hacks, why not use an actual tool for the job? If you want to parse C++, then use a C++ parser, such as libclang.

  • I think there is no easy way. I don't want to use a parser for this as it is a simple project with a complex issue, though. The regex I use gives me 90% accurate results. I think I'll be fine with that. – SS Sid Feb 24 '16 at 20:37