Say I have this String in java:
This a "text". This is inches 30". And another 20\"
I want to find all inches (double quotes after a number) and add a second escape for it, if it doesn't have yet.
So after the change, the string would be like this, printed in console:
This is a "text". This is inches 30\". And another 20\"
So the regex must check if there is a number before the double quote and if there is not a escape character already...
thanks!!
Try with Positive Lookbehind
String str="This a \"text\". This is inches 30\". And another 20\\\"";
System.out.println(str.replaceAll("(?<=\\d)\"", "\\\\\""));
output:
This a "text". This is inches 30\". And another 20\"
Here is Online demo as well.
Pattern explanation:
(?<= look behind to see if there is:
\d digits (0-9)
) end of look-behind
\" '"'