?
Solved

How to Split nested () praceket

Posted on 2014-07-24
7
Medium Priority
?
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

 
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 2000 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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

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.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month8 days, 8 hours left to enroll

764 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