进行了全面搜索,以了解如何替换字符串中的带引号的值。 (我希望这是一个普遍的要求!)
考虑以下简单示例:
String test1 = "This is a string with a \"dog\" and a \"cat\" that are unwanted.";
System.out.println(test1);
System.out.println(test1.replaceAll("\".*\"", "ZAPPED"));
产生:
This is a string with a "dog" and a "cat" that are unwanted.
This is a string with a ZAPPED that are unwanted.
但是我想要:
This is a string with a ZAPPED and a ZAPPED that are unwanted.
我希望模式匹配在第二个引号处停止(至少在replaceAll()中)。