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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:24:42+00:00 2026-05-23T20:24:42+00:00

Regex dialect: Java The problem: given a string, replace all occurrences of a substring

  • 0

Regex dialect: Java
The problem: given a string, replace all occurrences of a substring inside it, except when these occurrences are inside quotes.

Example1:

string: "test substr 'test substr' substr"
substring: "substr"
replacement: "YYYY"
output: "test YYYY 'test substr' YYYY"

Example2:

string: "test sstr 'test sstr' sstr"
substring: "substr"
replacement: "YYYY"
output: "test sstr 'test sstr' sstr"

Example3:

string: "test 'test substr'"
substring: "substr"
replacement: "YYYY"
output: "test 'test substr'"

This is my best try thus far:

Regex: ((?:[^']*'[^']+')*?[^']*?)substring
Replace: $1replacement

The problem with it is that it needs a substring outside the quotes after the last string within the quotes otherwise it doesn’t work, so Example3 will fail (output: “test ‘test YYYY'”).

Many thanks for your 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-05-23T20:24:42+00:00Added an answer on May 23, 2026 at 8:24 pm

    Here’s a way:

    public class Main {
        public static void main(String [] args) {
    
            String[] tests = {
                    "test substr 'test substr' substr",
                    "test sstr 'test sstr' sstr",
                    "test 'test substr'"
            };
    
            String regex = "substr(?=([^']*'[^']*')*[^']*$)";
    
            for(String t : tests) {
                System.out.println(t.replaceAll(regex, "YYYY"));
            }
        }
    }
    

    prints:

    test YYYY 'test substr' YYYY
    test sstr 'test sstr' sstr
    test 'test substr'
    

    Note that this does not work if ' can be escaped with a \ for example.

    A quick explanation:

    The following: ([^']*'[^']*')* will match 0 or an even number of single quotes with non quotes in between, and [^']*$ matches any non-quotes and the end-of-string.

    So, the complete regex substr(?=([^']*'[^']*')*[^']*$) matches any "substr" that has 0 or an even number of single quotes ahead of it, when looking all the way to the end-of-string!

    Looking all the way to the end-of-string is the key here. If you wouldn’t do that, the following "substr" would also be replaced:

    aaa 'substr' bbb 'ccc ddd' eee
               ^     ^       ^
               |     |       |
               i     ii     iii
    

    because it “sees” an even number of single quotes ahead of it (i and ii). You must force it to look at the entire string to the right of it (all the way to $)!

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

Sidebar

Related Questions

It seems the .NET Regex.Replace method automatically replaces all matching occurrences. I could provide
What regex pattern would need I to pass to java.lang.String.split() to split a String
Regex Pattern - ([^=](\\s*[\\w-.]*)*$) Test String - paginationInput.entriesPerPage=5 Java Regex Engine Crashing / Taking
I need a Regex that will match a java method declaration. I have come
Regex rx = new Regex(@[+-]); string[] substrings = rx.Split(expression); expression = -9a3dcbh-3bca-4ab4cf-3hc //This is
I don't know Regex very well, and I'm trying to get all of the
Regex \w doesn't match utf-8 characters in Ruby 1.9.2. Anybody faced same problem? Example:
Regex really does nothing if i run this code: input contains: geeeeekdldn Regex.Replace(input, @g(.|\n)*?n,
Regex for not allowing single quotes and double quotes in javascript .
Regex parsing problem, I am putting together an embed video provider module for Drupal.

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.