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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:44:37+00:00 2026-06-04T03:44:37+00:00

I need do find all multiline comments in a string and replace them with

  • 0

I need do find all multiline comments in a string and replace them with a space (if the comment is in one line) or with a \n (if the comment is on more than one line).
for example:

int/* one line comment */a;

should be changed to:

int a;

and this:

int/* 
more
than one
line comment*/a;

should be changed to:

int
a;

I have one String with all the text and I used this command:

file = file.replaceAll("(/\\*([^*]|(\\*+[^*/]))*\\*+/)"," ");

where file is the string.

The problem is it finds all multiline comment and I want to separate it to 2 cases.
How can I do it?

  • 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-04T03:44:39+00:00Added an answer on June 4, 2026 at 3:44 am

    This can be solved using Matcher.appendReplacement and Matcher.appendTail.

    String file = "hello /* line 1 \n line 2 \n line 3 */"
                + "there /* line 4 */ world";
    
    StringBuffer sb = new StringBuffer();
    Matcher m = Pattern.compile("(?m)/\\*([^*]|(\\*+[^*/]))*\\*+/").matcher(file);
    
    while (m.find()) {
    
        // Find a comment
        String toReplace = m.group();
    
        // Figure out what to replace it with
        String replacement = toReplace.contains("\n") ? "\n" : "";
    
        // Perform the replacement.
        m.appendReplacement(sb, replacement);
    }
    
    m.appendTail(sb);
    
    System.out.println(sb);
    

    Output:

    hello 
    there  world
    

    Note: If you want to preserve correct line number / columns for all text that is not inside comments (good if you want to refer back to the source code in error messages etc) I would recommend doing

    String replacement = toReplace.replaceAll("\\S", " ");
    

    which replaces all non-whitespace with white space. This way \n is preserved, and

    "/* abc */"
    

    is replaced by

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

Sidebar

Related Questions

Basically I need to find all the ids with more than 1 name so
I need to find all lines in myfile.txt containing word 'MyWord', and then replace
I need to find all substrings from a string that starting with a given
I have an application where I need to parse a string to find all
I need to find all dijit.form.CheckBox widgets inside a DIV and enable/disable them all.
I have a string. In this string i need replace all special characters (0-31
I have a List<string> which has some words duplicated. I need to find all
I am given a string and i need to find all possible letter combinations
I need to find all sub-arrays which share any mutual element and merge them
I need to find all divs that are set to style=display:none and change them

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.