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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:38:34+00:00 2026-05-28T00:38:34+00:00

I’d like to manipulate a String in Java using Regex. Goal is to find

  • 0

I’d like to manipulate a String in Java using Regex. Goal is to find all $ signs that have an even number of \ in front of them (or none) and then add another \.

Example:

"$ Find the $ to \$ escape \\$ or not \\\$ escape \\\\$ like here $"

should result in:

"\$ Find the \$ to \$ escape \\\$ or not \\\$ escape \\\\\$ like here \$"

Rationale here is: some $ are already escaped with a \ and some escape \ might be in the string as well in the form of \\. I need to escape the remaining $.

  • 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-28T00:38:34+00:00Added an answer on May 28, 2026 at 12:38 am

    This should do the work: replace:

    (^|[^\\])(\\{2})*(?=\$)
    

    with the whole text matched (except for the lookahead), followed by \\.

    Illustration in perl:

    $ perl -pe 's,(^|[^\\])(\\{2})*(?=\$),$&\\,g'
    "$ Find the $ to \$ escape \\$ or not \\\$ escape \\\\$ like here $" # in...
    "\$ Find the \$ to \$ escape \\\$ or not \\\$ escape \\\\\$ like here \$" # out
    "\$ Find the \$ to \$ escape \\\$ or not \\\$ escape \\\\\$ like here \$" # in...
    "\$ Find the \$ to \$ escape \\\$ or not \\\$ escape \\\\\$ like here \$" # out
    

    With Java, the whole text match is $0. Sample code:

    // package declaration skipped
    import java.util.regex.Pattern;
    
    public final class TestMatch
    {
        private static final Pattern p
            = Pattern.compile("(^|[^\\\\])(\\\\{2})*(?=\\$)");
    
        public static void main(final String... args)
        {
            String input = "\"$ Find the $ to \\$ escape \\\\$ or not \\\\\\$ "
                + "escape \\\\\\\\$ like here $\"";
    
            System.out.println(input);
    
            // Apply a first time
            input = p.matcher(input).replaceAll("$0\\\\");
            System.out.println(input);
    
            // Apply a second time: the input should not be altered
            input = p.matcher(input).replaceAll("$0\\\\");
            System.out.println(input);
            System.exit(0);
        }
    }
    

    Output:

    "$ Find the $ to \$ escape \\$ or not \\\$ escape \\\\$ like here $"
    "\$ Find the \$ to \$ escape \\\$ or not \\\$ escape \\\\\$ like here \$"
    "\$ Find the \$ to \$ escape \\\$ or not \\\$ escape \\\\\$ like here \$"
    

    A little explanation about the regex used is in order:

                    # begin regex:
    (               # start group
        ^           # find the beginning of input,
        |           # or
        [^\\]       # one character which is not the backslash
    )               # end group
                    # followed by
    (               # start group
        \\{2}       # exactly two backslashes
    )               # end group
    *               # zero or more times
                    # and at that position,
    (?=             # begin lookahead
        \$          # find a $
    )               # end lookahead
                    # end regex
    

    To be really complete, here are the positions at which the regex engine will find matching text (symbolized with <>) and the cursor position (symbolized by |):

    # Before first run:
    |"$ Find the $ to \$ escape \\$ or not \\\$ escape \\\\$ like here $"
    # First match
    "<>|$ Find the $ to \$ escape \\$ or not \\\$ escape \\\\$ like here $"
    # Second match
    "$ Find the <>|$ to \$ escape \\$ or not \\\$ escape \\\\$ like here $"
    # Third match
    "$ Find the $ to \$ escape <\\>|$ or not \\\$ escape \\\\$ like here $"
    # Fourth match
    "$ Find the $ to \$ escape \\$ or not \\\$ escape <\\\\>|$ like here $"
    # From now on, there is no match
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have thousands of HTML files to process using Groovy/Java and I need to
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have some data like this: 1 2 3 4 5 9 2 6

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.