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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:46:54+00:00 2026-05-26T00:46:54+00:00

Say I have a file, that contains some text. There are substrings like substr1,

  • 0

Say I have a file, that contains some text. There are substrings like “substr1”, “substr2”, “substr3” etc. in it. I need to replace all of those substrings with some other text, like “repl1”, “repl2”, “repl3”. In Python, I would create a dictionary like this:

{
 "substr1": "repl1",
 "substr2": "repl2",
 "substr3": "repl3"
}

and create the pattern joining the keys with ‘|’, then replace with re.sub function.
Is there a similar simple way to do this in Java?

  • 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-26T00:46:54+00:00Added an answer on May 26, 2026 at 12:46 am

    This is how your Python-suggestion translates to Java:

    Map<String, String> replacements = new HashMap<String, String>() {{
        put("substr1", "repl1");
        put("substr2", "repl2");
        put("substr3", "repl3");
    }};
    
    String input = "lorem substr1 ipsum substr2 dolor substr3 amet";
    
    // create the pattern joining the keys with '|'
    String regexp = "substr1|substr2|substr3";
    
    StringBuffer sb = new StringBuffer();
    Pattern p = Pattern.compile(regexp);
    Matcher m = p.matcher(input);
    
    while (m.find())
        m.appendReplacement(sb, replacements.get(m.group()));
    m.appendTail(sb);
    
    
    System.out.println(sb.toString());   // lorem repl1 ipsum repl2 dolor repl3 amet
    

    This approach does a simultanious (i.e. “at once”) replacement. I.e., if you happened to have

    "a" -> "b"
    "b" -> "c"
    

    then this approach would give "a b" -> "b c" as opposed to the answers suggesting you should chain several calls to replace or replaceAll which would give "c c".


    (If you generalize this approach to create the regexp programatically, make sure you Pattern.quote each individual search word and Matcher.quoteReplacement each replacement word.)

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

Sidebar

Related Questions

Let's say I have a tab-delimited text file that contains data arranged in columns
Let's say I have an existing trivial XML file named 'MyData.xml' that contains the
Let's say I have a text file that is 100 lines long. I want
Say, I have a file that contains 100000 integers. Each integer is on a
(PHP 5.3.6) I have a php file which contains simply this text - there
I have a .rtf file that contains nothing but an integer, say 15. I
Let's say we have a binary file that contains 2 bytes that form an
Say I have a data file that I want to process; I want to
For example, let's say I have a Maven build file that has dependencies located
Lets say I have 10 lines in a file. I have 2 parameters that

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.