java replaceall函数_java基础—->String中replace和replaceAll方法

这里面我们分析一下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("29", "--"));

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('\\') == -1) && (s.indexOf('$') == -1))

return s;

StringBuilder sb = new StringBuilder();

for (int i=0; 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 < 0)

throw new IllegalStateException("No match available");

// Process substitution string to replace group references with groups

int cursor = 0;

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 = -1;

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() == 0)

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(0)))

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 - '0';

if ((refNum < 0)||(refNum > 9))

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) - '0';

if ((nextDigit < 0)||(nextDigit > 9)) { // not a number

break;

}

int newRefNum = (refNum * 10) + nextDigit;

if (groupCount() < newRefNum) {

done = true;

} else {

refNum = newRefNum;

cursor++;

}

}

}

// Append group

if (start(refNum) != -1 && end(refNum) != -1)

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;

}

友情链接

https://www.cnblogs.com/huhx/tag/java/default.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>