Saturday, 31 August 2013

Replace All String literals in a line in Java using Regex

Replace All String literals in a line in Java using Regex

Is there any regex in java which can convert the following input into the
output
Input:
System.out.println("This is \" line number one"); System.out.println("This
is line number two"); String x = "This is string x";
Output:
System.out.println("ABC"); System.out.println("ABC"); String x = "ABC";
Notes:
1) I am basically reading a java file line by line using StringBuffer and
replacing ALL the string literals with a string of my choice.
2) Since there can be exceptional conditions like multiple println
statement on a single line or something sort of like that, or there can be
quote characters inside the string itself following an escape character, I
have used the above mentioned sample input.
3) I had posted a similar question in the forum, but I guess i wasn't able
to explain my requirement properly there. And I'm not completely aware of
"duplicate question" policies here... So please pardon me and take
necessary actions, but please first try to answer my question. Here is the
link to my previous question regarding this requirement: My Previous
Question
4) I know what my requirement is, so please be kind enough to spare me
from answers like "why are you doing this way... why do you NEED to do
this... "

No comments:

Post a Comment