I wrote some java code, might be ugly, but it works...
package Testing;
public class TestReplaceWithQuotes {
public static String replaceInsideQuotes(String source, char quote,
String target, String replacement){
StringBuilder regex = new StringBuilder();
StringBuilder replace = new StringBuilder();
String quoteChar = Character.isDigit(quote) || Character.
isAlphabetic(quote) ? String.valueOf(quote) : ("\" + quote);
String nonQuoteChar = "[^" + quoteChar + "]";
// System.out.println(quoteChar);
// System.out.println(nonQuoteChar);
regex.append("(");
regex.append("^");
regex.append(nonQuoteChar + "*");
regex.append("(" + quoteChar + nonQuoteChar + "*" + quoteChar +
nonQuoteChar + "*" + ")" + "*?");
regex.append(quoteChar + nonQuoteChar + "*");
regex.append(")");
regex.append("(");
regex.append(target);
regex.append(")");
regex.append("(");
regex.append(".*");
regex.append("$");
regex.append(")");
// System.out.println(regex.toString());
// Pattern pattern = Pattern.compile(regex.toString());
// Matcher matcher = pattern.matcher(source);
//
// if(matcher.matches()){
// for(int i = 0; i <= matcher.groupCount(); i++){
// System.out.println(String.valueOf(i) + ": " + matcher.
group(i));
// }
// }
replace.append("$1");
replace.append(replacement);
replace.append("$4");
// System.out.println(replace.toString());
String from = regex.toString();
String to = replace.toString();
String oldString = source;
String newString = "";
while(true){
newString = oldString.replaceAll(from, to);
if(oldString.equals(newString)){
break;
}else{
oldString = newString;
}
}
return oldString;
}
public static String replaceOutsideQuotes(String source, char quote,
String target, String replacement){
StringBuilder regex = new StringBuilder();
StringBuilder replace = new StringBuilder();
String quoteChar = Character.isDigit(quote) || Character.
isAlphabetic(quote) ? String.valueOf(quote) : ("\" + quote);
String nonQuoteChar = "[^" + quoteChar + "]";
regex.append("(");
regex.append("^");
regex.append(nonQuoteChar + "*");
regex.append("(" + quoteChar + nonQuoteChar + "*" + quoteChar +
nonQuoteChar + "*" + ")" + "*?");
regex.append(nonQuoteChar + "*");
regex.append(")");
regex.append("(");
regex.append(target);
regex.append(")");
regex.append("(");
regex.append(".*");
regex.append("$");
regex.append(")");
replace.append("$1");
replace.append(replacement);
replace.append("$4");
String from = regex.toString();
String to = replace.toString();
String oldString = source;
String newString = "";
while(true){
newString = oldString.replaceAll(from, to);
if(oldString.equals(newString)){
break;
}else{
oldString = newString;
}
}
return oldString;
}
public static void main(String[] args){
String source = ""我是张老三,我来自东北,我们家乡有高粱和大米,[以
下删去一百字]",张老三自豪的说,"张老三我在东北老出名了,[以下删去四百字]",
张老三又自豪的说.";
String target = "张老三";
String replacementIn = "博导";
String replacementOut = "博妹";
System.out.println(source);
String result = source;
result = replaceInsideQuotes(result, '"', target, replacementIn);
result = replaceOutsideQuotes(result, '"', target, replacementOut);
System.out.println(result);
}
}