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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:05:43+00:00 2026-06-15T16:05:43+00:00

I am trying to solve a codingbat problem using regular expressions whether it works

  • 0

I am trying to solve a codingbat problem using regular expressions whether it works on the website or not.

So far, I have the following code which does not add a * between the two consecutive equal characters. Instead, it just bulldozes over them and replaces them with a set string.

public String pairStar(String str) {
    Pattern pattern = Pattern.compile("([a-z])\\1", Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(str);
    if(matcher.find())
    matcher.replaceAll(str);//this is where I don't know what to do
    return str;
}

I want to know how I could keep using regex and replace the whole string. If needed, I think a recursive system could help.

  • 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-15T16:05:43+00:00Added an answer on June 15, 2026 at 4:05 pm

    This works:

    while(str.matches(".*(.)\\1.*")) {
        str = str.replaceAll("(.)\\1", "$1*$1");
    }
    return str;
    

    Explanation of the regex:

    The search regex (.)\\1:

    • (.) means “any character” (the .) and the brackets create a group – group 1 (the first left bracket)
    • \\1, which in regex is \1 (a java literal String must escape a backslash with another backslash) means “the first group” – this kind of term is called a “back reference”

    So together (.)\1 means “any repeated character”

    The replacement regex $1*$1:

    • The $1 term means “the content captured as group 1”

    Recursive solution:

    Technically, the solution called for on that site is a recursive solution, so here is recursive implementation:

    public String pairStar(String str) {
        if (!str.matches(".*(.)\\1.*")) return str;
        return pairStar(str.replaceAll("(.)\\1", "$1*$1"));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to solve a problem from codingbat.com with regular expressions. I'm new
I'm trying to solve the following problem in Redis. I have a list that
I am trying to solve the following problem with Puppet: I have multiple nodes.
I am currently trying to solve a problem, that I have using Hibernate 4
I have been trying to solve this problem for a while, could not find
I am trying to solve the Josephus problem , and I have working code.
Trying to solve this problem: I have the following set of divs that when
Trying to solve a problem with templatetags. I have two templatetags: @register.inclusion_tag('directory/_alphabet.html') def alphabet_list(names):
I am trying to solve this problem http://www.spoj.pl/problems/PEBBMOV/ . I think I have the
Trying to solve a very simple problem using mvvm-light, but after days of sifting

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.