I am a newbie in using Regexp
Can anyone please provide required regexp to find all C++ code lines inside a C++ source code file used to declare a variable?
Some of the methods variables can be declared could be as mentioned below:
int x;
int y=9;
int x,y;
int x,y=0;
int x,y=0,c;
short const x=9;
int* x,y,c=null;
short const *x=NULL;
MyObj aobj();
MyObj aobj(x,y);
Myobj * a = dynamic_cast<A *>(b);
String x = "test";
The following regex will get you started:
breakdown:
(int|short|MyObj|String)= It will search for any of the listed items. A|can be added to the list along with more options..*(;)= all characters up to the first;, basically meaning to the end of the line.The problem is, there can be many ways to declare a variable. In your example alone there are 4 (5 if you count for case-sensitivity) ways to declare. Unless this is just a quick one time thing, I would look for what Richard mentioned about getting a complete parser. I’m not familiar with
C++enough to help you with that.