java基础---->String中replace和replaceAll方法

时间:2023-03-15 23:10:35

  这里面我们分析一下replace与replaceAll方法的差异以及原理。

replace各个方法的定义

一、replaceFirst方法

public String replaceFirst(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceFirst(replacement);
}

二、replace方法

public String replace(CharSequence target, CharSequence replacement) {
return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
}

三、replaceAll方法

public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}

replace各个方法的原理

我们通过以下的例子来分析他们的原理。

@Test
public void stringReplace() {
replaceFirst("year = 1929. month=07, day=29, other=\\d{2}");
} public void replaceFirst(String string) {
System.out.println(string.replaceFirst("\\d{2}", "--")); System.out.println(string.replace("\\d{2}", "--"));
System.out.println(string.replace("", "--")); System.out.println(string.replaceAll("\\d{2}", "--"));
} // year = --29. month=07, day=29, other=\d{2}
// year = 1929. month=07, day=29, other=--
// year = 19--. month=07, day=--, other=\d{2}
// year = ----. month=--, day=--, other=\d{2}

一、首先我们分析一下replaceFirst与replaceAll方法,他们的区别在于Pattern构建之后Matcher调用的方法不同。一个是reaplceFirst、一个是replaceAll方法。这两个方法现在可以分析一下。

1、首先对于Matcher的replceFirst方法:可以看到只调用一下的appendReplacement和appendTail方法。关于appendReplacement方法后面可以贴出源码,实现比较复杂

public String replaceFirst(String replacement) {
if (replacement == null)
throw new NullPointerException("replacement");
reset();
if (!find())
return text.toString();
StringBuffer sb = new StringBuffer();
appendReplacement(sb, replacement);
appendTail(sb);
return sb.toString();
}

2、对于Matcher的replceAll方法,和上述的replaceFirst方法类似。只不过是多次调用了appendReplacement的替换函数。直到没有匹配为止

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();
}

二、对于replace方法,和上述的replaceAll方法主要有以下两种区别。

1、在Pattern.compile时,添加了Pattern.LITERAL的flag,表示pattern会把regex当作纯文本来处理了。比如\\d{2}不转义成两个0-9的数字,而是当作纯文本\\d{2}看待。

2、在调用MatcherMatcher.quoteReplacement(replacement.toString())方法对replacement做了对特殊符号($和\)作去除转义的操作。

public static String quoteReplacement(String s) {
if ((s.indexOf('\\') == -) && (s.indexOf('$') == -))
return s;
StringBuilder sb = new StringBuilder();
for (int i=; i<s.length(); i++) {
char c = s.charAt(i);
if (c == '\\' || c == '$') {
sb.append('\\');
}
sb.append(c);
}
return sb.toString();
}

但是为何只对\\和$做处理呢?

三、以下是我们的重点appendReplacement方法

 public Matcher appendReplacement(StringBuffer sb, String replacement) {

     // If no match, return error
if (first < )
throw new IllegalStateException("No match available"); // Process substitution string to replace group references with groups
int cursor = ;
StringBuilder result = new StringBuilder(); while (cursor < replacement.length()) {
char nextChar = replacement.charAt(cursor);
if (nextChar == '\\') {
cursor++;
if (cursor == replacement.length())
throw new IllegalArgumentException("character to be escaped is missing");
nextChar = replacement.charAt(cursor);
result.append(nextChar);
cursor++;
} else if (nextChar == '$') {
// Skip past $
cursor++;
// Throw IAE if this "$" is the last character in replacement
if (cursor == replacement.length())
throw new IllegalArgumentException("Illegal group reference: group index is missing");
nextChar = replacement.charAt(cursor);
int refNum = -;
if (nextChar == '{') {
cursor++;
StringBuilder gsb = new StringBuilder();
while (cursor < replacement.length()) {
nextChar = replacement.charAt(cursor);
if (ASCII.isLower(nextChar) ||
ASCII.isUpper(nextChar) ||
ASCII.isDigit(nextChar)) {
gsb.append(nextChar);
cursor++;
} else {
break;
}
}
if (gsb.length() == )
throw new IllegalArgumentException("named capturing group has 0 length name");
if (nextChar != '}')
throw new IllegalArgumentException("named capturing group is missing trailing '}'");
String gname = gsb.toString();
if (ASCII.isDigit(gname.charAt()))
throw new IllegalArgumentException("capturing group name {" + gname + "} starts with digit character");
if (!parentPattern.namedGroups().containsKey(gname))
throw new IllegalArgumentException("No group with name {" + gname + "}");
refNum = parentPattern.namedGroups().get(gname);
cursor++;
} else {
// The first number is always a group
refNum = (int)nextChar - '';
if ((refNum < )||(refNum > ))
throw new IllegalArgumentException("Illegal group reference");
cursor++;
// Capture the largest legal group string
boolean done = false;
while (!done) {
if (cursor >= replacement.length()) {
break;
}
int nextDigit = replacement.charAt(cursor) - '';
if ((nextDigit < )||(nextDigit > )) { // not a number
break;
}
int newRefNum = (refNum * ) + nextDigit;
if (groupCount() < newRefNum) {
done = true;
} else {
refNum = newRefNum;
cursor++;
}
}
}
// Append group
if (start(refNum) != - && end(refNum) != -)
result.append(text, start(refNum), end(refNum));
} else {
result.append(nextChar);
cursor++;
}
}
// Append the intervening text
sb.append(text, lastAppendPosition, first);
// Append the match substitution
sb.append(result); lastAppendPosition = last;
return this;
}

四、以下是appendTail的代码

public StringBuffer appendTail(StringBuffer sb) {
sb.append(text, lastAppendPosition, getTextLength());
return sb;
}

友情链接