Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9151585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:50:21+00:00 2026-06-17T11:50:21+00:00

I was curious on how it would be possible to split mathematical equations with

  • 0

I was curious on how it would be possible to split mathematical equations with parenthesis meaningfully using java’s string regex. It’s hard to explain without an example, one is below.

A generic solution pattern would be appreciated, rather than one which just works for the example provided below.

String s = "(5 + 6) + (2 - 18)";
// I want to split this string via the regex pattern of "+",
// (but only the non-nested ones) 
// with the result being [(5 + 6), (2 - 18)]

s.split("\\+"); // Won't work, this will split via every plus.

What I’m mainly looking for is first level splitting, I want a regex check to see if a symbol like “+” or “-” is nested in any form, if it is, don’t split it, if it isn’t split it. Nesting can be in the form of () or [].

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:50:22+00:00Added an answer on June 17, 2026 at 11:50 am

    If you don’t expect splitting nested expressions like ((6 + 5)-4), I have a pretty simple function to split the expressions without using regular expressions :

    public static String[] subExprs(String expr) {
        /* Actual logic to split the expression */
        int fromIndex = 0;
        int subExprStart = 0;
        ArrayList<String> subExprs = new ArrayList<String>();
        again:
        while ((subExprStart = expr.indexOf("(", fromIndex)) != -1) {
            fromIndex = subExprStart;
            int substringEnd=0;
            while((substringEnd = expr.indexOf(")", fromIndex)) != -1){
                subExprs.add(expr.substring(subExprStart, substringEnd+1));
                fromIndex = substringEnd + 1;
                continue again; 
            }
        }
    
        /* Logic only for printing */
        System.out.println("Original expression : " + expr);
        System.out.println();
        System.out.print("Sub expressions : [ ");
        for (String string : subExprs) {
            System.out.print(string + ", ");
        }
        System.out.print("]");
        String[] subExprsArray = {};
        return subExprs.toArray(subExprsArray);
    }
    

    Sample output :

    Original expression : (a+b)+(5+6)+(57-6)

    Sub expressions : [ (a+b), (5+6), (57-6), ]

    EDIT

    For the extra condition of also getting expressions enclosed in [], this code will handle expressions inside both () and [].

    public static String[] subExprs(String expr) {
    
        /* Actual logic to split the expression */
        int fromIndex = 0;
        int subExprStartParanthesis = 0;
        int subExprStartSquareBrackets = 0;
        ArrayList<String> subExprs = new ArrayList<String>();
        again: while ((subExprStartParanthesis = expr.indexOf("(", fromIndex)) > -2
                && (subExprStartSquareBrackets = expr.indexOf("[", fromIndex)) > -2) {
    
            /* Check the type of current bracket */
            boolean isParanthesis = false;
            if (subExprStartParanthesis == -1
                    && subExprStartSquareBrackets == -1)
                break;
            else if (subExprStartParanthesis == -1)
                isParanthesis = false;
            else if (subExprStartSquareBrackets == -1)
                isParanthesis = true;
            else if (subExprStartParanthesis < subExprStartSquareBrackets)
                isParanthesis = true;
    
            /* Extract the sub expression */
            fromIndex = isParanthesis ? subExprStartParanthesis
                    : subExprStartSquareBrackets;
            int subExprEndParanthesis = 0;
            int subExprEndSquareBrackets = 0;
            if (isParanthesis) {
                while ((subExprEndParanthesis = expr.indexOf(")", fromIndex)) != -1) {
                    subExprs.add(expr.substring(subExprStartParanthesis,
                            subExprEndParanthesis + 1));
                    fromIndex = subExprEndParanthesis + 1;
                    continue again;
                }
            } else {
                while ((subExprEndSquareBrackets = expr.indexOf("]", fromIndex)) != -1) {
                    subExprs.add(expr.substring(subExprStartSquareBrackets,
                            subExprEndSquareBrackets + 1));
                    fromIndex = subExprEndSquareBrackets + 1;
                    continue again;
                }
            }
        }
    
        /* Logic only for printing */
        System.out.println("Original expression : " + expr);
        System.out.println();
        System.out.print("Sub expressions : [ ");
        for (String string : subExprs) {
            System.out.print(string + ", ");
        }
        System.out.print("]");
        String[] subExprsArray = {};
        return subExprs.toArray(subExprsArray);
    }
    

    Sample Output :

    Original expression : (a+b)+[5+6]+(57-6)-[a-b]+[c-d]

    Sub expressions : [ (a+b), [5+6], (57-6), [a-b], [c-d], ]

    Do suggest improvements in the code. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why would I prefer using vector to deque I am curious why
I am curious it is possible to create a window without using WndProc. So
Just curious how you would comment this line of code: string customerNm = customerNm.EndsWith(s)
I'm curious if following code would be considered safe? using (SqlConnection cn = new
I'm curious, how would you test a string and say yep, that is a
I'm rather curious. Would it be possible to make something as complex as a
I'm curious if this is even possible with Regex. I want to extract tokens
I'm learning Scala and was curious if it would be possible to: Create an
I'm curious, what would be the neatest way to parse a string of xml
I am curious if it is possible to achieve a simple div swap using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.