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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:02:03+00:00 2026-05-14T06:02:03+00:00

This is similar to my previous efforts ( wordEnds and repeatEnd ): as a

  • 0

This is similar to my previous efforts (wordEnds and repeatEnd): as a mental exercise, I want to solve this toy problem using regex only.

Description from codingbat.com:

Given a string and a non-empty word string, return a version of the original string where all chars have been replaced by pluses ("+"), except for appearances of the word string which are preserved unchanged.

plusOut("12xy34", "xy") → "++xy++"
plusOut("12xy34", "1") → "1+++++"
plusOut("12xy34xyabcxy", "xy") → "++xy++xy+++xy"

There is no mention whether or not to allow overlap (e.g. what is plusOut("+xAxAx+", "xAx")?), but my non-regex solution doesn’t handle overlap and it passes, so I guess we can assume non-overlapping occurrences of word if it makes it simpler (bonus points if you provide solutions for both variants!).

In any case, I’d like to solve this using regex (of the same style that I did before with the other two problems), but I’m absolutely stumped. I don’t even have anything to show, because I have nothing that works.

So let’s see what the stackoverflow community comes up with.

  • 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-14T06:02:03+00:00Added an answer on May 14, 2026 at 6:02 am

    This passes all their tests:

    public String plusOut(String str, String word) {
      return str.replaceAll(
        String.format("(?<!(?=\\Q%s\\E).{0,%d}).", word, word.length()-1),
        "+"
      );  
    }
    

    Also, I get:

    plusOut("1xAxAx2", "xAx") → "+xAxAx+"
    

    If that’s the result you were looking for then I pass your overlap test as well, but I have to admit, that one’s by accident. 😀

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

Sidebar

Related Questions

This is similar to my previous posting. But this time I want to call
This is perhaps similar to previous posts, but I want to be specific about
This is similar to a previous question , but the answers there don't satisfy
This is Similar to a previous question I asked about the Cubic Bezier. I've
I'm using NetBeans IDE 6.9 (Build 201006101454), and wrote this simple Java class (that
I've been refactoring my models and controllers in an effort to remove code duplication,
I am developing a semi-complicated site that is available in several countries at once.
I am attempting to allow my web designers to use the metadata we have
Let's say I have a PHP Model-View-Controller framework that maps an address like http://example.com/admin/posts/edit/5

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.