I have a complete Java based code base, where members are named:
String m_sFoo;
Array m_arrKeepThings;
Variable/object names includes both a m_ prefix to indicate a member, and an hungarian notation type indicator.
I’m looking for a way to perform a single time code replacment to (for example on the above to cases):
Array keepThings;
String foo;
Of course there are many other alternatives, but I hope that based on two examples, I’ll be able to perform the full change.
Performances is not an issue as it’s a single time fix.
To clarify, if I had to explain this in lines, it would be:
- Match words starting with m_[a-zA-Z].
- After m_, drop whatever is there before the first Capital letter.
- Change the first capital letter to lower case.
Here is some Java code that works. It is not pure regex, but based on:
Usage:
NameMatcher.java
Demo Output:
Edit 0:
Since dollar signs (
$), micro (µ) and pound (£) are valid characters for Java name variables, I edited the regex.Edit 1: It seems that there are a lot of non-latin characters that are valid (
éùàçè, etc). Hopefully you don’t have to handle them.Edit 2: I’m only a human being! So be aware of errors there might be in the code! Make a BACKUP first!
Edit 3: Code improved. A NPE was thrown when the code contains this:
m_foo. These will be unhandled.