如何捕获和重用与Java正则表达式的匹配?

时间:2023-01-14 14:05:31

I'm trying to remember the correct notation for doing find-replace regex matches in Java.

我正在努力记住在Java中使用find-replace regex匹配的正确表示法。

Say I have the string

说我有字符串

    String s = "My name is ''Eric'' and I have a bee called ''Eric'' 
and a fish called ''Wanda''."

I want to do something like the following:

我想做类似以下的事情:

s.replaceAll("\'\'$$\'\'", "$$");

To give: My name is Eric and I have a bee called Eric and a fish called Wanda.

给:我的名字是Eric,我有一只名叫Eric的蜜蜂和一只名叫Wanda的鱼。

But I know $$ isn't the correct notation to capture whatever is in the '' and use it to replace the found match.

但我知道$$不是正确的符号来捕捉''中的任何内容并用它来代替找到的匹配。

What's the particular notation I'm looking for here?

我在这里找的是什么特别的符号?

Thanks in advance.

提前致谢。

-Dave.

戴夫。

1 个解决方案

#1


6  

s.replaceAll("\'\'(.*?)\'\'", "$1");

#1


6  

s.replaceAll("\'\'(.*?)\'\'", "$1");