Solved

How to Split nested () praceket

Posted on 2014-07-24
7
218 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

912 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now