Solved

How to Split nested () praceket

Posted on 2014-07-24
7
225 Views
Last Modified: 2014-10-15
Hi,

I want to Split String  
add(add(15,add(4,5)),5)

my result should be
add(4,5)
add(15,add(4,5))
add(add(15,add(4,5)),5)

how to do this ..? can any one give me some idea ?
0
Comment
  • 3
  • 2
  • 2
7 Comments
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 40218009
Its dynamic input so it may be different more than one nest bracket
eg:
add(add(15,add(4,add(5,4))),5)

or
add(add(15,5),5) how to do that??please help me give some tips !!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40218114
This is a duplicate of the following, essentially

http://www.experts-exchange.com/Programming/Languages/Java/Q_28482815.html
0
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 40218180
ya . correct. here i mention my question clearly. thats why i created new one.

i created this pattern
(\\(([^()]*|\\([^()]*\\))*\\)) its show the brackets but not nested . can you help me on this cehj???????
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 86

Expert Comment

by:CEHJ
ID: 40218205
I already have - the answer is the same as before
0
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 40218224
not only add will string it may verie length() . so depend upon on the add or length function only we decide its no or size

eg:
add(15,length(4,5)) output should be no(15)+size(4,5)
before split i need to find wich function it is? thats the pblm i am facing
0
 
LVL 4

Accepted Solution

by:
Nuno Rogado earned 500 total points
ID: 40219444
Hi Dravidnsr,

It works for your string, but I need to check for other scenarios... it's still a work in progress... needs to be optimized and refactored... but since I have a plane to catch I will write it down here for you... meanwhile I will have fun in the plane finishing it... (it's C# but I think it's easy to change to java)...

         
  string s = "add(add(15,add(4,5)),5)";

            string[] left = s.Split('(');
            //string[] arguments = s.Split(',');
            //string[] right = s.Split(')');

            int xL = left.Count<string>();            //    add                   add         15,add       4,5)),5)
            //int xA = arguments.Count<string>();     //    add(add(15            add(4       5))          5)
            //int xR = right.Count<string>();         //    add(add(15,add(4,5                ,5          

            Dictionary<string, string> result = new Dictionary<string,string>();

            int parentesesIndex = 0;
            int commaIndex = 0;
            string f = "";
            string value = "";

            for (int i = 0; i < xL; i++)
            {
                f = "";
                parentesesIndex = left[i].IndexOf(")");
                if (parentesesIndex > -1)
                {
                    for (int j = i - 1; i >= 0; j--)
                    {
                        if (left[j].Length > 0)
                        {
                            commaIndex = left[j].IndexOf(",");
                            if (commaIndex > 0)
                            {
                                f = left[j].Substring(commaIndex + 1, left[j].Length - commaIndex - 1) + "(";
                                left[j] = left[j].Substring(0, commaIndex);
                                break;
                            }
                            else
                            {
                                if ((j - 1) < 0)
                                {
                                    f = left[j] + "(";
                                    left[j] = "";
                                    break;
                                }
                                else
                                {
                                    f = left[j - 1] + "(" + left[j];
                                    left[j] = "";
                                    left[j - 1] = "";
                                    break;
                                }
                            }
                        }
                    }

                    if (result.Count > 0)
                    {
                        if (result.TryGetValue((result.Count).ToString(), out value))
                        {
                            commaIndex = left[i].Substring(0, parentesesIndex + 1).IndexOf(",");

                            if (commaIndex < 0)
                            {
                                f = f + "," + value + left[i].Substring(0, parentesesIndex + 1);
                            }
                            else 
                            {
                                f = f + value + left[i].Substring(0, parentesesIndex + 1);
                            }
                        }
                    }
                    else
                    {
                        f = f + left[i].Substring(0, parentesesIndex + 1);
                    }
                    result.Add((result.Count + 1).ToString(), f);

                    left[i] = left[i].Substring(parentesesIndex + 1, left[i].Length - parentesesIndex - 1);

                    i = i - 1;
                }
            }

Open in new window


I hope this answer will help you solving your problem.

Best Regards,
Nuno Rogado
0
 
LVL 4

Expert Comment

by:Nuno Rogado
ID: 40381781
Meanwhile I developed a recursive version of it, simpler and more effective, do you need it?
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 97
Apps blocked by Java 9 81
SHA2 certs for IIS AND Java? 2 94
Java: The Public Class Main 4 17
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question