Ok. So what you want is to search, within a String, for a sequence of characters starting and ending with double-quotes?

    String bar = "A \"car\"";
    Pattern string = Pattern.compile("\".*?\"");
    Matcher matcher = string.matcher(bar);
    String result = matcher.replaceAll("\"bicycle\"");

Note the non-greedy .*? pattern.

Answer from Wangnick on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › regex › literals.html
String Literals (The Java™ Tutorials > Essential Java Classes > Regular Expressions)
The most basic form of pattern matching supported by this API is the match of a string literal. For example, if the regular expression is foo and the input string is foo, the match will succeed because the strings are identical. Try this out with the test harness: Enter your regex: foo Enter ...
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › util › regex › Pattern.html
Pattern (Java Platform SE 7 )
Enables literal parsing of the pattern. When this flag is specified then the input string that specifies the pattern is treated as a sequence of literal characters.
🌐
Vogella
vogella.com › tutorials › JavaRegularExpressions › article.html
Regular expressions in Java - Tutorial
A simple example for a regular expression is a (literal) string. For example, the Hello World regex matches the "Hello World" string. . (dot) is another example for a regular expression. A dot matches any single character; it would match, for example, "a" or "1". The following tables lists ...
🌐
W3Schools
w3schools.com › java › java_regex.asp
Java Regular Expressions
Java does not have a built-in Regular Expression class, but we can import the java.util.regex package to work with regular expressions.
🌐
JRebel
jrebel.com › blog › java-regular-expressions-cheat-sheet
Java Regular Expressions (Regex) Cheat Sheet | JRebel
Let's move on to the syntax for Java Regex. The Pattern.compile method takes a String, which is the RegEx that defines a set of matching strings. Naturally, it has to have a tricky syntax, otherwise a single string defining the pattern can only represent itself.
🌐
Regular-Expressions.info
regular-expressions.info › java.html
Using Regular Expressions in Java
The literal string "\\" is a single backslash. In regular expressions, the backslash is also an escape character. The regular expression \\ matches a single backslash. This regular expression as a Java string, becomes "\\\\". That’s right: 4 backslashes to match a single one.
🌐
TutorialsPoint
tutorialspoint.com › pattern-literal-field-in-java-with-examples
Pattern LITERAL field in Java with examples
In literal mode, the metacharacter "^" has no meaning and the regular expression "^This" matches the exact word. import java.util.regex.Matcher; import java.util.regex.Pattern; public class LTERAL_Example { public static void main(String[] args) { String input = "This is the first line\n" + ...
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › core java › guide to escaping characters in java regexps
Guide to Escaping Characters in Java RegExps | Baeldung
July 22, 2024 - In this quick test, the Pattern.quote() method is used to escape the given regex pattern and transform it into a String literal. In other words, it escapes all the metacharacters present in the regex pattern for us.
🌐
YouTube
youtube.com › daniel ross
Learn Java Programming - Regex String Literals Tutorial - YouTube
In this tutorial I will discuss how string literals are the most simplistic form of pattern matching. The tutorial builds on concepts learned from my Introdu...
Published   February 1, 2016
Views   4K
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › regex › Pattern.html
Pattern (Java Platform SE 8 )
October 20, 2025 - Enables literal parsing of the pattern. When this flag is specified then the input string that specifies the pattern is treated as a sequence of literal characters.
🌐
GitHub
gist.github.com › cellularmitosis › 6fd5fc2a65225364f72d3574abd9d5d5
Matching a string literal using regex · GitHub
One way to think of this is that we disallow backslashes unless they are followed by another character. So, "\" is not a valid string, while "\\", "\t" and "\"" are valid strings. But there is one last gotcha: the . doesn't mean "any character", it actually means "any character other than a newline", so this regex won't work with multi-line strings.
🌐
CodeGym
codegym.cc › java blog › strings in java › regular expressions in java
Java RegEx Regular expressions | CodeGym
February 19, 2025 - String s = "The root directory is \nWindows"; // Move "Windows" to a new line String s = "The root directory is \u00A7Windows"; // Insert a paragraph symbol before "Windows" This means that string literals that describe regular expressions and use "\" characters (i.e. to indicate metacharacters) must repeat the backslashes to ensure that the Java bytecode compiler doesn't misinterpret the string. For example: String regex = "\\s"; // Pattern for matching a whitespace character String regex = "\"Windows\""; // Pattern for matching "Windows" Double backslashes must also be used to escape special characters that we want to use as "normal" characters.
🌐
Dev.java
dev.java › learn › regex › string-literals
String Literals - Dev.java
The most basic form of pattern matching supported by this API is the match of a string literal. For example, if the regular expression is foo and the input string is foo, the match will succeed because the strings are identical.
🌐
GNU
gnu.org › software › kawa › Regular-expressions.html
Kawa: Regular expressions
If we needed to match the character “.” itself, we escape it, ie, precede it with a backslash “\”. The character sequence “\.” is thus a metasequence, since it doesn’t match itself but rather just “.”. So, to match “a” followed by a literal “.” followed by “c” we use the regexp pattern “a\.c”. To write this as a Scheme string literal, you need to quote the backslash, so you need to write "a\\.c". Kawa also allows the literal syntax #/a\.c/, which avoids the need to double the backslashes. You can choose between two similar styles of regular expressions. The two differ slightly in terms of which characters act as metacharacters, and what those metacharacters mean: Functions starting with regex- are implemented using the java.util.regex package.
Top answer
1 of 4
5

Consider the following regular expression:

String regex = "\"(?:\\\\\"|[^\"])*?\"";

It starts with a quote, followed by zero or more non-quote characters or escaped quote characters. The last character has to be a quote.

If you apply this regex to java code, remember that it also matches text inside quotes in comments. If you have unbalanced quotes in your comments it won't match string literals (it will then match the exact opposite).

If you had the example you posted in a String variable named example the following would work:

String wanted = example.replaceAll(regex, "\"ABC\"");

Here's a full example:

String literal = "String foo = \"bar\" + \"with\\\"escape\" + \"baz\";";
String regex = "\"(?:\\\\\"|[^\"])*?\"";
String replacement = "\"\"";
String wanted = literal.replaceAll(regex, replacement);
System.out.println(literal);
System.out.println(wanted);

prints

String foo = "bar" + "with\"escape" + "baz";
String foo = "" + "" + "";
2 of 4
2

Based on Uri's answer of using the parser grammar in this question:

"(?:\\[\\'"tnbfru01234567]|[^\\"])*?"

as Java string:

"\"(?:\\\\[\\\\'\"tnbfru01234567]|[^\\\\\"])*?\""

Explanation (see also Java String escape sequences):

"                          // start with a double quote
  (?:                      // a non-capture group
    \\[\\'"tnbfru01234567] // either an escape sequence
  |                        // or
    [^\\"]                 // not an escape sequence start or ending double quote
  )*?                      // zero or more times, not greedy
"                          // ending double quote

Example (jlordo's solution fails on this):

    String literal = "String foo = \"\\\\\" + \"bar\" + \"with\\\"escape\" + \"baz\" + \"\\117\\143\\164\\141\\154\";";
    String regex = "\"(?:\\\\[\\\\'\"tnbfru01234567]|[^\\\\\"])*?\"";
    String replacement = "\"\"";
    String wanted = literal.replaceAll(regex, replacement);
    System.out.println(literal);
    System.out.println(wanted);