Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello guys,

I want to replace
Java
Logger.info(className, dasd + "sadsd" );


with

Java
Logger.info(className, dasd + "sadsd" + " [" + Thread.currentThread().getStackTrace()[1].getLineNumber() + "]");


using regex.


how to do this?
Thanks

What I have tried:

I've been able to identify the first string as
Logger.[a-z]+.\(([^)]+)\);


But cant create the replacement string
Posted
Updated 12-Jan-23 17:13pm
Comments
Richard MacCutchan 12-Jan-23 6:46am    
Why do you think a regex is simpler than the code you already have??
Kundan D 12-Jan-23 20:28pm    
I want to add the thread statement to all the loggers in our code base.
The logger follows a similar format as I've shown and there are thousands of loggers so I cant edit them one by one so regex is simpler I think

Given the pattern:
RegEx
Logger.([a-z]+)\(([^)]+)\);
then a replacement pattern of:
RegEx
Logger.$1($2 + " [" + Thread.currentThread().getStackTrace()[1].getLineNumber() + "]");
should do the trick.

Demo[^]
 
Share this answer
 
Comments
CPallini 12-Jan-23 7:07am    
5.
Kundan D 12-Jan-23 21:59pm    
This is close but doesnt work.

the dasd + "sadsd" string is representative, there can be absolutely anything there... so it will be better if you can accommodate all that into a single group. There can be nums, strings, parenthesis too. I need to cover all those cases, maybe i should've included that in my original question.
Richard Deeming 13-Jan-23 4:03am    
The pattern allows absolutely anything there except a closing bracket.
I think i got it to work

Search: Logger.([a-z]+)\(([a-z]+),((.*)+)\);

 

Replace with: 
Logger.$1($2, $3 + " [" + Thread.currentThread().getStackTrace()[1].getLineNumber() + "]");


Please feel free to suggest any improvements!

Thank you @Richard Deeming for showing the way
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900