Solved

How to Split nested () praceket

Posted on 2014-07-24
7
244 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
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

688 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