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 6989767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:15:23+00:00 2026-05-27T19:15:23+00:00

I want to replace i# (look that the example inputs/outputs) with the value of

  • 0

I want to replace i# (look that the example inputs/outputs) with the value of an array, but I’m not sure how to do this with Java+Regex.

Assume you have an array with: [3,2,1,0]

Example inputs:
i0
i1^2
(i1+2)+5
2*5+i1
i1+i2-i3
1+2

Example output:
3 [why? input is i0 and index 0 = 3 in the array]
2^2
(2+2)+5
2*5+2
2+1-0
1+2

Regex is here:
http://rubular.com/r/KXbCQnbs8K

REGEX = i{1}(\d+)

Code:

private String replace(String input){
    StringBuffer s = new StringBuffer();
    Pattern regex = Pattern.compile(REGEX);
    Matcher m = regex.matcher(input);

    if( !m.find(0) ){
        return input;
    }else{
        m.reset();
    }

    while (m.find() ){
        m.appendReplacement(s, getRealValue(m.group(1)) );
    }
    return s.toString();
}

private String getRealValue(String val){
    int value = Integer.parseInt(val);
    return String.valueOf(array.get(value));
}

Assume i#s given are always valid. My code works for some cases, but fails in most. Any help? Thanks!

EDIT:
I’m not sure how to tell it to add the last part (for example: +5 in i0+5).

i0 — works
i1^2 — doesn’t work
(i1+2)+5 — doesn’t work
2*5+i1 — works
i1+i2-i3 — doesn’t work
1+2 — works
1+i2 — works

I want to modify the regex to “i{1}(\d+)(.*)”
if(lastMatch()){ //if last match is true
s += m.group(2) //concat the last group (ie. “+5” in “i0+5”)
}
But I don’t know the correct syntax for that.

  • 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-27T19:15:24+00:00Added an answer on May 27, 2026 at 7:15 pm

    So it fails… what is it about the output that is being produced that is wrong? That’s a very useful bit of information that you’ve neglected to mentioned. In the future you should try to think more about why the output is wrong, this will help you to figure what the program is doing wrong.

    But by the looks of it you’ve forgotten to use Matcher.appendTail(StringBuffer) after you’ve done all the replacements. appendTail appends any remaining characters after the last match eg. "i0[this bit]".

    I assume the wrong output was

    • i1^2 -> 2
    • (i1+2)+5 -> (2

    Looking at this it would have been much faster to figure out what was going wrong. It’s forgetting to add last bit of String at the end. Let’s find a way to sort this out or read the API to see if there’s a method that does it in one simple step for me.

    Example code

    while (m.find() ){
        m.appendReplacement(s, getRealValue(m.group(1)) );
    }
    m.appendTail(s); // you missed out this line
    return s.toString();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use a look-ahead regex for replacement with the System.Text.RegularExpression.Replace(...) method. Now
I want to replace the first context of web/style/clients.html with the java String.replaceFirst method
The sad truth about this post is that I have poor regex skills. I
$str = preg_replace('\(look: (.{1,80})\)'Ui, (look: <a href=\dict.php?process=word&q=\\1\>\\1</a>),$str); i want to encode url, but how
I have a log file that has lines that look like this: 1,2546857-23541,f_last,user,4:19 P.M.,11/02/2009,START,27,27,3,c2546857-23541,
I want to delete some certain characters that used wrongly in a string. ........I.wanna.delete.only.the.dots.outside.of.this.text...............
I got the following Expression that can look like this (the amount of Sqrt[XXX]
I have a string and I want to replace every space in this string
I want to replace the 'client' field text box in fogbugz when you edit
I want to replace RBSplitView with NSSplitView in my existing project. The application is

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.