code: private String removeEnclosedCurlyBraces( String str ) {
          final char curlyReplacement = 6;
  
          char[] chars = str.toCharArray();
          int open = 0;
          for ( int i = 0; i < chars.length; i++ ) {
              if ( chars[i] == '{' ) {
                  if ( open != 0 ) chars[i] = curlyReplacement;
                  open++;
              }
              else if ( chars[i] == '}' ) {
                  open--;
                  if ( open != 0 ) {
                      chars[i] = curlyReplacement;
                  }
              }
          }
  
          char[] res = new char[chars.length];
          int j = 0;
          for( int i = 0; i < chars.length; i++ ) {
              if( chars[i] != curlyReplacement ) {
                  res[j++] = chars[i];
              }
          }
  
          return new String( Arrays.copyOf( res, j ) );
      }
docs: Enclosed curly braces cannot be matched with a regex. Thus we remove them before applying the replaceAll method