/**
* 依次替换占位符
* 例如: 姓名:{s},电话:{s},邮箱:{s} --> 姓名:小张,电话:18800000001,邮箱:abc@123.com
* pattern = "\\{s}";
*
* @param input
* @param pattern
* @param texts
* @param nullStr 不能为null
* @return
*/
public static String appendReplacement(String input, String pattern, String[] texts, String nullStr) {
// 从正则表达式实例中运行方法并查看其如何运行
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);
StringBuffer sb = new StringBuffer();
int i = 0;
while (m.find()) {
// 将匹配之前的字符串复制到sb,再将匹配结果替换掉,并追加到sb
m.appendReplacement(sb, i < texts.length && texts[i] != null ? texts[i] : nullStr);
i++;
}
m.appendTail(sb);
return sb.toString();
}
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 this matcher 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 than if itwere 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 = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "dog");
}
m.appendTail(sb);
System.out.println(sb.toString());

String替换占位符的更多相关文章

  1. 【Android 应用开发】Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片

    一. Application用途 1. Application用途 创建Application时机 : Application在启动的时候会调用Application无参的构造方法创建实例; Appl ...

  2. maven 编译替换占位符

    首先开启资源配置的插件,由此插件替换占位符 <plugin> <groupId>org.apache.maven.plugins</groupId> <art ...

  3. Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片

    一. Application用途 1. Application用途 创建Application时机 : Application在启动的时候会调用Application无参的构造方法创建实例; Appl ...

  4. Roslyn 使用 Target 替换占位符方式生成 nuget 打包

    本文告诉大家如何编写在编译过程修改打包文件 在项目文件的相同文件夹可以放一个 nuspec 用来告诉 VisualStudio 如何打包 现在尝试创建一个项目 NearjerbetearDeeyito ...

  5. spring中PropertyPlaceholderHelper替换占位符的值

    1.Properties中的值替换¥{}或者#{}占位符 String text = "foo=${foo},bar=${bar}"; Properties props = new ...

  6. Lua中string.format占位符的使用

    虽然lua中字符串拼接"string.format"相对于".."消耗较大,但有时为了代码的可读性,项目中还是经常用到"string.format&q ...

  7. 安卓程序代写 网上程序代写[原]Android项目中string.xml占位符

    开发中经常遇到这样的情况 , 在string.xml中用到以下占位符 <string name="delete_success">删除<xliff:g id=&q ...

  8. 【占位符替换】替换String中的占位符标志位{placeholder}

    概述 占位符替换, 占位符表示为:{placeholder}; 示例:替换如下{xxx}占位符中的内容 "名字:{name},年龄:{age},学校:{school}" 提供了两种 ...

  9. spring源码解析(一)---占位符解析替换

    一.结构类图 ①.PropertyResolver : Environment的顶层接口,主要提供属性检索和解析带占位符的文本.bean.xml配置中的所有占位符例如${}都由它解析 ②.Config ...

随机推荐

  1. [转帖]K8s 学习者绝对不能错过的最全知识图谱(内含 58个知识点链接)

    K8s 学习者绝对不能错过的最全知识图谱(内含 58个知识点链接)   https://www.cnblogs.com/alisystemsoftware/p/11429164.html 需要加强学习 ...

  2. Django 用Session和Cookie分别实现记住用户登录状态

    简介 由于http协议的请求是无状态的.故为了让用户在浏览器中再次访问该服务端时,他的登录状态能够保留(也可翻译为该用户访问这个服务端其他网页时不需再重复进行用户认证).我们可以采用Cookie或Se ...

  3. 题解 CF546C 【Soldier and Cards】

    思路 是一道水题,可以用队列+模拟来写,注意不要拿完队列中的元素! 代码 #include<iostream> #include<cstdio> #include<que ...

  4. PHP学习之PHP编码习惯

    命名的注意事项: 命名要有实际含义 命名风格保持一致 不用拼音命名 不用语言关键字 适当的使用注释 好的代码应该是自描述的 难以理解的地方加上注释 函数的功能加上注释说明 类的功能和使用方法加注释 多 ...

  5. 4.Linux系统命令及其使用详解

    运维工程师必会的109个Linux命令   文件管理basename:从文件名中去掉路径和扩展名 cat:把档案串连接后传到基本输出(屏幕或加 > filename 到另一个档案)cd:切换目录 ...

  6. kubernetes 水平伸缩及yaml格式编写

    Replication Controller:用来部署.升级PodReplica Set:下一代的Replication ControllerDeployment:可以更加方便的管理Pod和Repli ...

  7. mac 在finder上面显示完成路径

    打开终端,输入以下命令并回车: defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES 然后再把finder关了再打开,你会 ...

  8. 【PR笔记】一、打造希区柯克变焦效果

    1. 导入素材,“链接选择项”关闭,删除音频 2. 添加效果--视频效果--扭曲--视频稳定器, 然后程序帮我们自动稳定 3.视频首尾添加关键帧,首帧缩放200%  尾帧不变, 使视频前后的主体大小差 ...

  9. 微信小程序点击图片预览-wx.previewImage

    <view class='imgList'> <view class='imgList-li' wx:for='{{imgArr}}'> <image class='im ...

  10. 7.移动端自动化测试-小知识 try...except...finally语句

    异常Error 我们在写代码的时候,经常会遇见程序抛出Error无法执行的情况 一般情况下,在Python无法正常处理程序时就会发生一个异常.异常是Python对象,表示一个错误.当Python脚本发 ...