JayR
JayR

Reputation: 441

Replace Character inside brackets

Hi to all coders out there. Let me do this straight.

I want to replace all character in a string inside a opening and closing brackets.

what I have in my code.

string value= str.replaceAll("\\[.*\\]", "strToReplace"); 

It's working , but the problem is it also delete the brackets.

ex:

 I want to say [Hello].

the result:

 I want to say .

How can I achieve that? Anyone? Thanks in Advance. It will be a really big help in me.

Upvotes: 3

Views: 105

Answers (2)

Giru Bhai
Giru Bhai

Reputation: 14408

string= str.replaceAll("\[.*\]", "strToReplace")

to

String s = str.replaceAll("\\[(.*?)\\]","[" + "strToReplace" + "]");

Upvotes: 1

rubberdont
rubberdont

Reputation: 474

String s = str.replaceAll("\[.*\]", "strToReplace")

to

String s = str.replaceAll("\[.*?\\]", "["+"strToReplace"+"]")

Upvotes: 2

Related Questions