Solved

How to Split nested () praceket

Posted on 2014-07-24
7
210 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

747 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

10 Experts available now in Live!

Get 1:1 Help Now