Regex to add hungarian notation
I'm parsing .h
and .cpp
files and I need to find/replace all non-Hungaria开发者_开发技巧n notated variables with their Hungarian equivalents. "Augh, why?!" you ask? My employer requires Hungarian notation, 'nuff said.
Let's just deal with ints
for now.
Given any of these cases...
int row; // no hungarian prefix
int nrow(9); // incorrect capitalization
int number; // hmm...
int nnumber = getValue(); // uh oh!
They should be changed to:
int nRow;
int nRow(9); // obviously ctor args and assignments shouldn't change
int nNumber;
int nNumber = getValue();
I'm shooting for just a single one-line call to s///
ideally.
For added challenge, if someone can get something to change ALL instances of this variable after the "type check" with int
, that would earn you some brownie points.
Here's what I have so far:
s/(int\s+)(?!n)(\w+)/$1n\u$2/g;
This doesn't match things like int nrow
or int number
though.
Thanks in advance!
No.
More explicitly, you are trying to compile a program using a regex. This does not work that way.
For example, your one-line already forgets of function parameters, and it cannot parse any user defined type (struct, enum). Not to mention that .cpp suggests C++ suggest classes.
Also, what happens with method / function / inline comments?
My advice would be find somewhere a grammar compiler and pass it the c++ grammar, so for every definition you get the value and type written down to a file. Then you can have regex fun with it. You may try also to write every time each variable is used in order to replace them later automatically.
Yet a lot more complex that a simple regex, but that simple regex will fail so much that at the end you will be changing the code manually.
As a positive note, maybe when you tell your boss how much the chane does cost maybe he will think about it better.
精彩评论