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
Based on Uri's answer of using the parser grammar in this question:
"(?:\\[\\'"tnbfru01234567]|[^\\"])*?"
as Java string:
"\"(?:\\\\[\\\\'\"tnbfru01234567]|[^\\\\\"])*?\""
Explanation (see also Java String escape sequences):
" // start with a double quote
(?: // a non-capture group
\\[\\'"tnbfru01234567] // either an escape sequence
| // or
[^\\"] // not an escape sequence start or ending double quote
)*? // zero or more times, not greedy
" // ending double quote
Example (jlordo's solution fails on this):
String literal = "String foo = \"\\\\\" + \"bar\" + \"with\\\"escape\" + \"baz\" + \"\\117\\143\\164\\141\\154\";";
String regex = "\"(?:\\\\[\\\\'\"tnbfru01234567]|[^\\\\\"])*?\"";
String replacement = "\"\"";
String wanted = literal.replaceAll(regex, replacement);
System.out.println(literal);
System.out.println(wanted);
Consider the following regular expression:
String regex = "\"(?:\\\\\"|[^\"])*?\"";
It starts with a quote, followed by zero or more non-quote characters or escaped quote characters. The last character has to be a quote.
If you apply this regex to java code, remember that it also matches text inside quotes in comments. If you have unbalanced quotes in your comments it won't match string literals (it will then match the exact opposite).
If you had the example you posted in a String
variable named example
the following would work:
String wanted = example.replaceAll(regex, "\"ABC\"");
Here's a full example:
String literal = "String foo = \"bar\" + \"with\\\"escape\" + \"baz\";";
String regex = "\"(?:\\\\\"|[^\"])*?\"";
String replacement = "\"\"";
String wanted = literal.replaceAll(regex, replacement);
System.out.println(literal);
System.out.println(wanted);
prints
String foo = "bar" + "with\"escape" + "baz";
String foo = "" + "" + "";
You can use this also \b(?:(?<=")[^"]*(?=")|\w+)\b
. This will find all the strings which surrounded with Double qoutes
("example").
Sample Code:
String line="\"Hello\" World"
Pattern pattern = Pattern.compile("\b(?:(?<=\")[^\"]*(?=\")|\w+)\b");
Matcher matcher = pattern.matcher(line);
while(matcher.find()) {
//replace the string with you string
}
The output will be Actual line: "Hello" World Answer : ABC World
s = s.replaceAll("\"([^\n\"\\]+|\\\\.)*\"", "\"ABC\"");
This searches quote, and any either non-quotes/non-backslahes/non-linefeeds or backslash+character, till quote.
\"
(
[^\n\"\\]+
|
\\\\.
)*
\"
[^ ... ]
non of the enclosed chars, range possible too A-Z
.|
or..
any character, by default not line endings.... +
one or more of ... .... *
zero or more of ... .