Reputation: 15564
I my java app, I have a following character sequence: b"2
(any single character, followed by a double quote followed by a single-digit number)
I need to replace the double quote with a single quote character. I'm trying this:
Pattern p = Pattern.compile(".\"d");
Matcher m = p.matcher(initialOutput);
String replacement = m.replaceAll(".'d");
This does not seem to do anything.
What is the right way of doing this?
Upvotes: 1
Views: 30
Reputation: 50776
First off, d
represents a literal character. You're looking for \d
, which represents a numeric digit.
The other issue is that you're replacing variable characters with the string literal ".'d"
. One solution is to capture the variable portions and reference them in the replacement:
String replacement = initialOutput.replaceAll("(.)\"(\\d)", "$1'$2");
Another approach is to use lookarounds to check the surrounding characters without actually matching them for replacement:
String replacement = initialOutput.replaceAll("(?<=.)\"(?=\\d)", "'");
Upvotes: 1