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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:14:37+00:00 2026-06-15T10:14:37+00:00

Below I am trying to replace exact search strings with a replace string xxx.

  • 0

Below I am trying to replace exact search strings with a replace string xxx. I have string original which resembles a java log message. As you can see if the search string is “variable” I am able to successfully replace it with xxx but when the search string is “variable.method()” or “variable.method2()” I am not able to replace it. Am I doing something wrong here?

val original = """variable+ " is in " + variable.method() + " and " + variable.method2()"""
val replaceStr = "xxx"
val searchStr = "variable.method()" // This can change like in the output below
val variableExtracter = ("(?<=^|\\s|\\+)"+searchStr+"(?=\\s|$|\\+)").r
print("Search string : " + searchStr + "\n")
print("Original :" + original + "\n")
val formatted = variableExtracter.replaceAllIn(original, replaceStr)
print("Formatted :" + formatted + "\n")

Output:

Search string : variable //CORRECT OUTPUT
Original :variable+ " is in " + variable.method() + " and " + variable.method2()
Formatted :xxx+ " is in " + variable.method() + " and " + variable.method2()
---
Search string : variable.method()
Original :variable+ " is in " + variable.method() + " and " + variable.method2()
Formatted :variable+ " is in " + variable.method() + " and " + variable.method2()
//EXPECTED OUTPUT: variable+ " is in " + xxx + " and " + variable.method2()
---
Search string : variable.method2()
Original :variable+ " is in " + variable.method() + " and " + variable.method2()
Formatted :variable+ " is in " + variable.method() + " and " + variable.method2()
//EXPECTED OUTPUT:variable+ " is in " + variable.method() + " and " + xxx 
---
Search string : variable.method()
Original :variable.method()+ " has " + variable + " and " + variable.method2()
Formatted :variable.method()+ " has " + variable + " and " + variable.method2()
//EXPECTED OUTPUT:xxx+ " has " + variable + " and " + variable.method2()
  • 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-15T10:14:38+00:00Added an answer on June 15, 2026 at 10:14 am

    Edit:

    You can use the Java Pattern quote method Pattern.quote(searchStr) to wrap your string with the \Q and \E.

    scala> import java.util.regex.Pattern
    import java.util.regex.Pattern
    
    scala> val variableExtracter = ("(?<=^|\\s|\\+)"+Pattern.quote(searchStr)+"(?=\\s|$|\\+)").r
    variableExtracter: scala.util.matching.Regex = (?<=^|\s|\+)\Qvariable.method2()\E(?=\s|$|\+)
    
    scala> print("Search string : " + searchStr + "\n")
    Search string : variable.method2()
    
    scala> print("Original :" + original + "\n")
    Original :variable+ " is in " + variable.method() + " and " + variable.method2()
    
    scala> val formatted = variableExtracter.replaceAllIn(original, replaceStr)
    formatted: String = variable+ " is in " + variable.method() + " and " + xxx
    
    scala> print("Formatted :" + formatted + "\n")
    Formatted :variable+ " is in " + variable.method() + " and " + xxx
    
    scala> val searchStr = "variable.method()"
    searchStr: java.lang.String = variable.method()
    
    scala> val variableExtracter = ("(?<=^|\\s|\\+)"+Pattern.quote(searchStr)+"(?=\\s|$|\\+)").r
    variableExtracter: scala.util.matching.Regex = (?<=^|\s|\+)\Qvariable.method()\E(?=\s|$|\+)
    
    scala> val formatted = variableExtracter.replaceAllIn(original, replaceStr)
    formatted: String = variable+ " is in " + xxx + " and " + variable.method2()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to figure out how to replace binary data using Java. below is
I'm trying to trim an exception message with the below code: Response.Redirect(IllegalCharactersError.aspx?error=); string message
I am trying to replace certain parts of the string below. '''<td align=center> 5
I am trying below code but getting error String x = aaa XXX bbb;
I'm just trying to replace a string \\ with \\\\ Below is the program
In the below code i am trying to replace the contents of a file
I am trying to replace accented characters with the normal replacements. Below is what
I am trying to replace a string with a new string in a python
I am trying to replace all numbers in a <number></number> element as xxx if
I am trying to replace a string with another string, but the greedy nature

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.