0

I have this task where in the message has nested curly brackets.My motive is to get to remove only the inner most curly brackets and the rest of the message stays the same.A sample of The string message is as follows :

enter code here Input :  {4:{CLIENT ACCOUNT} :-}
                Output:  {4: CLIENT ACCOUNT :-}

So Basically we need to ensure to remove the innermost curly brackets,rest of the content staying the same.How to go about it ?

I was able to remove one level of braces using the following pseudo code :

enter code here
String str ="{CLIENT ACCOUNT}";
String regex = "(\\{|\\})"; 
str = str.replaceAll(regex, "");
System.out.println("Formatted String is--"+str);

but i am stuck as to what to use the regex for ignoring the first level of curly brackets.any help will be highly appreciated.

2 Answers 2

1

I don't know how to do this using a java regex, but you could do something like this:

String str = "someContent";
String newStr = "";
int level = 0;
for (int i = 0; i < str.length(); ++i){
    if (str.charAt(i) == '{'){
        if (level == 0) //check before incrementing
            newStr += "{";
        level++;
    } else if (str.charAt(i) == '}'){
        level--;
        if (level == 0) //check after incrementing
            newStr += "}";
    } else {
        newStr += str.charAt(i);
    }
}

return newStr;

You basically step through each character in the string and remember how many '{'s and '}'s you have already seen. Then you only print them out if you are at a net count of zero (or the outermost brackets)

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Harrison, Your java code when integrated into the program seems to be working pretty well.thanks a ton.I have been struggling with the regex past fw days but to no avail.I'll take your answer as the accepted answer.Cheers
0

Not the prettiest of answers..it won't work with deeply nested curly brackets, but it will work with multiple sets of nested curly brackets.

(\{[^}]+?|\G[^{]*)\{([^}]+)\}
$1 $2

Demo

{1:{CLIENT ACCOUNT}, 2:{FOO {OOPS} BAR}, 3:{TEST}}
{1: CLIENT ACCOUNT, 2: FOO {OOPS BAR}, 3: TEST}

In the above demo, you can see the error caused when we have a multi-nested set of curly brackets. This is because we assume the contents will be [^}]+, or anything but a closing bracket. Here is an expanded explanation:

(        (?# start capture)
  \{     (?# match { literally)
  [^}]+? (?# lazily match 1+ non-})
 |       (?# OR)
  \G     (?# start back from last match, still in a curly bracket)
  [^{]*  (?# match 0+ non-{ characters)
)        (?# end capture)
\{       (?# match { literally)
(        (?# start capture)
  [^}]+  (?# match 1+ non-} characters)
)        (?# end capture)
\}       (?# match } literally)

3 Comments

thx for the detailed explanation sam.There's only two level of nesting.not more than that.how to go about it in that case.i tried using your answer but it's not running as expected.
By "two level of nesting" are you referring to the {FOO {OOPS} BAR} in my example {1:{CLIENT ACCOUNT}, 2:{FOO {OOPS} BAR}, 3:{TEST}}? If so, what do you expect the outcome to be..please update OP :)
Sam ,by two levels of nesting i mean this : if input is : {4:{CLIENT ACCOUNT} :-} then output be : {4:CLIENT ACCOUNT:-}. Please check my updated question.There it mentions clearly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.