java 占位符替换_String替换占位符

Implements a non-terminal append-and-replace step.

This method performs the following actions:1. It reads characters from the input sequence, starting at theappend position, and appends them to the given string buffer. Itstops after reading the last character preceding the previous match,that is, the character at index start() - 1.2. It appends the given replacement string to the string buffer.3. It sets the append position of thismatcher to the index ofthe last character matched, plus one, that is, to end().

The replacement string may contain references to subsequencescaptured during the previous match: Each occurrence of ${name} or $gwill be replaced by the result of evaluating the corresponding group(name) or group(g)respectively. For $g,the first number after the $ is always treated as part ofthe group reference. Subsequent numbers are incorporated into g ifthey would form a legal group reference. Only the numerals‘0‘through ‘9‘ are considered as potential components of the groupreference. If the second group matched the string "foo", forexample, then passing the replacement string "$2bar" wouldcause "foobar"to be appended to the string buffer. A dollarsign ($) may be included as a literal in the replacementstring by preceding it with a backslash (\$).

Note that backslashes (\) and dollar signs ($) inthe replacement string may cause the results to be different thanifitwere being treated as a literal replacement string. Dollar signs may betreated as references to captured subsequences as described above, andbackslashes are used to escape literal characters in the replacementstring.

This method is intended to be used in a loop together with the appendTail and find methods. Thefollowing code,for example, writes one dog two dogs in theyard to the standard-output stream:

Pattern p= Pattern.compile("cat");

Matcher m= p.matcher("one cat two cats in the yard");

StringBuffer sb= newStringBuffer();while(m.find()) {

m.appendReplacement(sb,"dog");

}

m.appendTail(sb);

System.out.println(sb.toString());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值