I am looking for the regex which will let me use search replace to add 2 lines of code to all java methods in file using Notepad++
Before:
public void startProcessing() { .............. .............. }
After:
public void startProcessing() {
logger.info( "Entering into startProcessing" ); .............. .............. logger.info( "Exiting startProcessing" ); }
Is this possible, if yes can anyone help me with the code… or guide me on any other possible way to do this.
To start with the “bad news”, you won’t be able to insert the “Exiting” line unless you have a much-better fingerprint to match against. With your current code-sample, the best you can match against is
}and, as a wild guess, there are probably a lot of closing-curly-brackets throughout your code.To insert the “Starting” line is do-able, but the robustness depends on your input.
If you will always want to replace the same line as in your sample code (or the same format but different function name), you could do the following in the Find+Replace menu:
Find:
Replace:
Search Mode:
ExtendedIf you want to dynamically do the replacement with a non-hardcoded function name, you could try the following:
Find:
Replace:
Search Mode:
Regular ExpressionThis “dynamic” method will require whatever methods you’re searching for the be declared in the same format though,
public void functionName.... I’ve used[a-zA-Z0-9_$]as the character-set for the function names, but you can adjust this to suit your needs.UPDATE (ignore get/set methods)
To ignore get/set methods, such as
getFieldValueUnits()orsetFieldValueUnits(int val), you can use the followingFindvalue (theReplaceis the same-as-above):This will match all functions that do not start with
getorset(and are declared aspublic void, as above).