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

  • SEARCH
  • Home
  • 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 7645569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:58:26+00:00 2026-05-31T09:58:26+00:00

I can replace ABC(10,5) with (10)%(5) using: replaceAll(ABC\\(([^,]*)\\,([^,]*)\\), ($1)%($2)) but I’m unable to figure

  • 0

I can replace ABC(10,5) with (10)%(5) using:

replaceAll("ABC\\(([^,]*)\\,([^,]*)\\)", "($1)%($2)")

but I’m unable to figure out how to do it for ABC(ABC(20,2),5) or ABC(ABC(30,2),3+2).

If I’m able to convert to ((20)%(2))%5 how can I convert back to ABC(ABC(20,2),5)?

Thanks,
j

  • 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-05-31T09:58:27+00:00Added an answer on May 31, 2026 at 9:58 am

    I am going to answer about the first question. I was not able to do the task in a single replaceAll. I don’t think it is even achievable. However if I use loop then this should do the work for you:

        String termString = "([0-9+\\-*/()%]*)";
        String pattern = "ABC\\(" + termString + "\\," + termString + "\\)";
        String [] strings = {"ABC(10,5)", "ABC(ABC(20,2),5)", "ABC(ABC(30,2),3+2)"};
        for (String str : strings) {
            while (true) {
                String replaced = str.replaceAll(pattern, "($1)%($2)");
                if (replaced.equals(str)) {
                    break;
                }
                str = replaced;
            }
            System.out.println(str);
        }
    

    I am assuming you are writing parser for numeric expressions, thus the definition of term termString = "([0-9+\\-*/()%]*)". It outputs this:

    (10)%(5)
    ((20)%(2))%(5)
    ((30)%(2))%(3+2)
    

    EDIT As per the OP request I add the code for decoding the strings. It is a bit more hacky than the forward scenario:

        String [] encoded = {"(10)%(5)", "((20)%(2))%(5)", "((30)%(2))%(3+2)"};
        String decodeTerm = "([0-9+\\-*ABC\\[\\],]*)";
        String decodePattern = "\\(" + decodeTerm + "\\)%\\(" + decodeTerm + "\\)";
        for (String str : encoded) {
            while (true) {
                String replaced = str.replaceAll(decodePattern, "ABC[$1,$2]");
                if (replaced.equals(str)) {
                    break;
                }
                str = replaced;
            }
            str = str.replaceAll("\\[", "(");
            str = str.replaceAll("\\]", ")");
            System.out.println(str);
        }
    

    And the output is:

    ABC(10,5)
    ABC(ABC(20,2),5)
    ABC(ABC(30,2),3+2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I replace abc with abcd on all files of a folder using
Is there a built-in .NET class that can replace or work like WinHttp.WinHttpRequest? Thanks!
How can i use same file (like ABC.txt ) to write using UTL_FILE .
$var1 = 'abc'; $var2 = '123'; How can I replace %var1 and %var2% from
Can i replace a big text like During the day, abc is working. with
I can transition to a Fragment class using the code below, but how do
The string abc/def/* or abc/def/*/xyz How can I use preg_replace_callback to replace everything after
so 1GvG:s/..../g can replace over an entire buffer However, suppose I have multiple vim
I need to know how I can replace the last s from a string
I'd like a regexp or other string which can replace everything except alphanumeric chars

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.