开发者

How do you capture and reuse a match with Java regex?

开发者 https://www.devze.com 2023-03-07 04:26 出处:网络
开发者_如何学编程I\'m trying to remember the correct notation for doing find-replace regex matches in Java.

开发者_如何学编程I'm trying to remember the correct notation for doing find-replace regex matches in Java.

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.

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.


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

精彩评论

暂无评论...
验证码 换一张
取 消