如何用相同的定界符替换重复模式以打开和关闭模式?

时间:2019-02-05 16:24:08

标签: java regex replaceall

进行了全面搜索,以了解如何替换字符串中的带引号的值。 (我希望这是一个普遍的要求!)

考虑以下简单示例:

        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()中)。

0 个答案:

没有答案