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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:46:20+00:00 2026-06-03T04:46:20+00:00

I have strings which look something like this: You may use the promotion until

  • 0

I have strings which look something like this: “You may use the promotion until [ Start Date + 30]”. I need to replace the [ Start Date + 30] placeholder with an actual date – which is the start date of the sale plus 30 days (or any other number). [Start Date] may also appear on its own without an added number. Also any extra whitespaces inside the placeholder should be ignored and not fail the replacement.

What would be the best way to do that in Java? I’m thinking regular expressions for finding the placeholder but not sure how to do the parsing part. If it was just [Start Date] I’d use the String.replaceAll() method but I can’t use it since I need to parse the expression and add the number of days.

  • 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-03T04:46:21+00:00Added an answer on June 3, 2026 at 4:46 am

    You should use a StringBuffer and Matcher.appendReplacement and Matcher.appendTail

    Here’s a complete example:

    String msg = "Hello [Start Date + 30] world [ Start Date ].";
    StringBuffer sb = new StringBuffer();
    
    Matcher m = Pattern.compile("\\[(.*?)\\]").matcher(msg);
    
    while (m.find()) {
    
        // What to replace
        String toReplace = m.group(1);
    
        // New value to insert
        int toInsert = 1000;
    
        // Parse toReplace (you probably want to do something better :)
        String[] parts = toReplace.split("\\+");
        if (parts.length > 1)
            toInsert += Integer.parseInt(parts[1].trim());
    
        // Append replaced match.
        m.appendReplacement(sb, "" + toInsert);
    }
    m.appendTail(sb);
    
    System.out.println(sb);
    

    Output:

    Hello 1030 world 1000.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string (pulled from the web) which may look like this: \something\to\do.
I have to work with strings which may contain Lat/Long data, like this: $query
I have a string which may contain cell address, which is look like: A1,
I faced an interesting problem today. I have 4 strings which I need to
I'm building an application whose usage is going to look something like this: application
Suppose i have a post which is something like TITLE: WEB: SEO in 2011
I have a number of strings which contain words which are bunched together and
Example of the problem If I have a list of valid option strings which
Possible Duplicate: A better way to compare Strings which could be null I have
I have two strings in a java program, which I want to mix in

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.