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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:12:57+00:00 2026-05-26T08:12:57+00:00

If the source string contains the pattern, then replace it with something or remove

  • 0

If the source string contains the pattern, then replace it with something or remove it. One way to do it is to do something like this

Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(sourceString);
while(m.find()){
  String subStr = m.group().replaceAll('something',""); // remove the pattern sequence
  String strPart1 = sourceString.subString(0,m.start());
  String strPart2 = sourceString.subString(m.start()+1);
  String resultingStr = strPart1+subStr+strPart2;
  p.matcher(...);
}

But I want something like this

Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(sourceString);
    while(m.find()){
      m.group.replaceAll(...);// change the group and it is source string is automatically updated    
}

Is this possible?

Thanks

  • 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-26T08:12:58+00:00Added an answer on May 26, 2026 at 8:12 am

    // change the group and it is source string is automatically updated

    There is no way what so ever to change any string in Java, so what you’re asking for is impossible.

    To remove or replace a pattern with a string can be achieved with a call like

    someString = someString.replaceAll(toReplace, replacement);
    

    To transform the matched substring, as seems to be indicated by your line

    m.group().replaceAll("something","");
    

    the best solution is probably to use

    • A StringBuffer for the result
    • Matcher.appendReplacement and Matcher.appendTail.

    Example:

    String regex = "ipsum";
    String sourceString = "lorem ipsum dolor sit";
    
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(sourceString);
    StringBuffer sb = new StringBuffer();
    
    while (m.find()) {
        // For example: transform match to upper case
        String replacement = m.group().toUpperCase();
        m.appendReplacement(sb, replacement);
    }
    
    m.appendTail(sb);
    
    sourceString = sb.toString();
    
    System.out.println(sourceString); // "lorem IPSUM dolor sit"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a method Regex.Replace(string source, string pattern, string replacement) The last parameter supports
I have a string that contains multiple parameters delimited by #, like this :
I have a string read from another source such as \b\bfoo\bx. In this case,
Some open source libraries have tendency to re implement basic structures like string, list,
My open-source Android application uses this in an SQL query: String.format(%f, someDoubleValue); Unfortunately, in
I'm working this source code: #include <string> #include <vector> #include <iostream> #include <istream> #include
//This source is a line read from a file String src = 23570006,music,**,wu(),1,exam,\Monday9,10(H2-301)\,1-10,score,; //This
I want to remove all javascript codes from a string that contains html code.
I have a string that contains source code of an other groovy file in
I'd like an efficient method that would work something like this EDIT: Sorry I

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.