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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:59:11+00:00 2026-05-31T15:59:11+00:00

I need to replace some words in a text, but I need to put

  • 0

I need to replace some words in a text, but I need to put conditions in the replacement strategy as follows:

I want to replace word1 with word2:

String word1 = "word1";
String word2 = "word2";

but I don’t want to replace word1 if it’s preceded by word3 which is:

String word3 = "word3."; //with the dot at the ending

That is if the text is word3.word1 I don’t want to touch it.
But I can’t seem to handle that with word boundaries using String‘s replaceAll method.

EDIT:

And also I don’t want to change if word1 has a prefix or suffix of “-” character i.e. -word1 or word1- or -word1-

Any help would be appreciable.

  • 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-31T15:59:12+00:00Added an answer on May 31, 2026 at 3:59 pm

    I think this will give you a hint

    String str = "word3.word1.word2.word1";
    str.replaceAll("word3.word1", "word3.wordX1").replaceAll("word1", "word2").replaceAll("wordX1", "word1");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string containing HTML and I need to replace some words to
I want create Word document from another Word document, but I need replace some
I need to replace some text in a JNLP file using a DOS batch
The original words $string = '<a href=/home/top>here is some text</a>. <a href=/links/abc>some links here</a>...';
I have a string: $string = '<a href=/news/blablalba>some text</a>'; I need to add target=_blank
I need to write some code that will search and replace whole words in
I have some lxml element: >> lxml_element.text 'hello BREAK world' I need to replace
I have a string like some text <p>any text</p> from which I need to
I have trouble using sed. I need to replace some lines in very deprecated
I need to replace character (say) x with character (say) P in a string,

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.