tags:

views:

117

answers:

3

I need a regular expression for finding class declarations so I can add a #define before the "class" keyword. The regular expression doesn't have to be perfect, just good enough that it catches most of the cases.

+2  A: 

How about:

/^\s*class\s/

That should work reasonably well. I'm not quite sure.

Robert Massaioli
+1  A: 
/class\s+([^\s]+)/

This will capture the class name as $1 (or the equivalent in whatever regex framework you're using). This will not work for template classes that may have spaces inside the <> that follows the class name. Parsing that is something not doable with a regular expression, since it requires matching balanced pairs of angle brackets.

JSBangs
+2  A: 
/.*class\s+([^{]+)\s*[:]?([^{]+){/

This should work for most class definitions, including template classes. The class name is captured in $1 and if it is a derived class, the base will be in $2.

Yannick M.