日期:2014-05-20 浏览次数:20941 次
String s = "工作\r";
System.out.println("s = "+s);
System.out.println("s1 = "+s.replaceAll("\r", "r"));
System.out.println("s2 = "+s.replaceAll("\r", "\r"));
System.out.println("s3 = "+s.replaceAll("\r", "\\r"));
public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}
public String replaceAll(String replacement) {
reset();
boolean result = find();
if (result) {
StringBuffer sb = new StringBuffer();
do {
appendReplacement(sb, replacement);//看下面这个方法
result = find();
} while (result);
appendTail(sb);
return sb.toString();
}
return text.toString();
}
...
...
...
public Matcher appendReplacement(StringBuffer sb, String replacement) {
// If no match, return error
if (first < 0)
throw new IllegalStateException("No match available");
// Process substitution string to replace group references with groups
int cursor = 0;
String s = replacement;
StringBuffer result = new StringBuffer();
while (cursor < replacement.length()) {
char nextChar = replacement.charAt(cursor);
if (nextChar == '\\') {//这块对'\'的处理是第一次遇到这个'\',跳过了。直接取后边的字符。
cursor++;
nextChar = replacement.charAt(cursor);
result.append(nextChar);
cursor++;
} else if (nextChar == '$') {
// Skip past $
cursor++;
// The first number is always a group
int refNum = (int)replacement.charAt(cursor) - '0';
if ((refNum < 0)
------解决方案--------------------
(refNum > 9))
throw new IllegalArgumentException(
"Illegal group reference");
cursor++;
// Capture the largest legal group string
boolean done = false;