In my program I will be reading a java file line by line, and if there is any string literal in that line, i will replace it with (say) "ABC".
Is there any regex to do so?
Ex. If the Java file passed to my program is:
public class TestClass {
private static final boolean isNotThis = false;
public static void main(String[] args) {
String x = "This is a test String";
dummyMethodCall();
if(isNotThis){
makeItThat();
System.out.println("work is done");
}
}
}
Then the output java file should be:
public class TestClass {
private static final boolean isNotThis = false;
public static void main(String[] args) {
String x = "ABC";
dummyMethodCall();
if(isNotThis){
makeItThat();
System.out.println("ABC");
}
}
}
I am willing to know the regex that will help me to detect all string literals and replace them with a particular string of my choice.
EDIT:
The real challenge for me is to avoid those quote-characters inside a string. (if somebody puts a quote character with an escape character inside the string)
"\".*\""solves most of the cases. You may want to be worried about word boundary and lookback.