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.
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.
this regex can handle double quotes as well (NOTE: perl extended syntax):
"
[^\\"]*
(?:
(?:\\\\)*
(?:
\\
"
[^\\"]*
)?
)*
"
it defines that each " has to have an odd amount of escaping \ before it
maybe it's possible to beautify this a bit, but it works in this form
Add a + behind the number part in order for the regex to match numbers of any length. [0-9] alone will only match exactly 1 digit.
Furthermore, what about spaces? In your example there are spaces, in your code, there are none. You can add \\s* to match any (including none) white-space.
string.replaceAll("\"name\"\\s*:\\s*\"[0-9]+\",", "")
You can play around with it on Regex101.
Andy Turner's comment: You need to use replaceAll instead of replace. replace does not interpret the first parameter as a regex, but tries to find that exact string in your string.
this will do it for you
string.replaceAll( "\"name\"\\s*:\\s*\"\\d+\"", "" )
example:
final String string = "Some\"name\" : \"12345\"String";
System.out.println( string.replaceAll( "\"name\"\\s:\\s\"\\d+\"", "" )
will print the output:
SomeString
And it will work for any number
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 = "" + "" + "";
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);