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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:25:56+00:00 2026-06-18T05:25:56+00:00

I have a string, for ex: There exists a word *random*. random will be

  • 0

I have a string, for ex:

There exists a word *random*.

random will be a random word.
How can I use a regular expression to replace every character of random with * and have this result:

There exists a word ********.

So the * replaces every character, in this case 6 characters.
Notice that I am after to replace only the word random, not the surroundings *.
So far I have:

str.replaceAll("(\\*)[^.]*(\\*)", "\\*");

But it replaces *random* with *, instead of the desired ******** (total of 8).
Any help, really appreciated…

  • 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-18T05:25:57+00:00Added an answer on June 18, 2026 at 5:25 am

    If you have just a single word like that: –

    As far as current example is concerned, if you are having just a single word like that, then you can save yourself from regex, by using some String class methods: –

    String str = "There exists a word *random*.";
    
    int index1 = str.indexOf("*");
    int index2 = str.indexOf("*", index1 + 1);
    
    int length = index2 - index1 - 1;   // Get length of `random`
    
    StringBuilder builder = new StringBuilder();
    
    // Append part till start of "random"
    builder.append(str.substring(0, index1 + 1));
    
    // Append * of length "random".length()
    for (int i = 0; i < length; i++) {
        builder.append("*");
    }
    
    // Append part after "random"
    builder.append(str.substring(index2));
    
    str = builder.toString();
    

    If you can have multiple words like that: –

    For that, here’s a regex solution (This is where it starts getting a little complex): –

    String str = "There exists a word *random*.";
    str = str.replaceAll("(?<! ).(?!([^*]*[*][^*]*[*])*[^*]*$)", "*");
    System.out.println(str);
    

    The above pattern replaces all the characters that is not followed by string containing even numbers of * till the end, with a *.

    Whichever is appropriate for you, you can use.

    I’ll add an explanation of the above regex: –

    (?<! )       // Not preceded by a space - To avoid replacing first `*`
    .            // Match any character
    (?!          // Not Followed by (Following pattern matches any string containing even number of stars. Hence negative look-ahead
        [^*]*    // 0 or more Non-Star character
        [*]      // A single `star`
        [^*]*    // 0 or more Non-star character
        [*]      // A single `star`
    )*           // 0 or more repetition of the previous pattern.
    [^*]*$       // 0 or more non-star character till the end.     
    

    Now the above pattern will match only those words, which are inside a pair of stars. Provided you don’t have any unbalanced stars.

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

Sidebar

Related Questions

How to check for a string that for every character in it, there exists
I have this string: \Blah \'Blah\' Blah\ . There is another string inside it.
A little back story : I have small application that will use Word to
I have this vague recollection reading about groovy that there exists a '??' sugar
So, I have a file called 'dummy' which contains the string: There is 100%
I have the string temp[256] and inside it there's a double number, such as
I have a String Object in format yyyyMMdd .Is there a simple way to
I have a string that i need to split where there are 2 or
I have 1 string contaning html and one string contaning css is there any
I have a string which is in the following format, When there is a

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.