/**
* 依次替换占位符
* 例如: 姓名:{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. hdoj3534(树形dp,求树的直径的条数)

    题目链接:https://vjudge.net/problem/HDU-3534 题意:给出一棵树,求树上最长距离(直径),以及这样的距离的条数. 思路:如果只求直径,用两次dfs即可.但是现在要求最 ...

  2. 剑指offer21:第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。(注意:这两个序列的长度是相等的)

    1 题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是 ...

  3. 输入一个正整数n,输出所有和为n的连续正整数序列

    public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { System. ...

  4. 5.Linux常用排查命令

    可以使用一下命令查使用内存最多的10个线程     ps aux | sort -k4nr | head -n 10   可以使用一下命令查使用CPU最多的10个线程     ps aux | sor ...

  5. WPF入门(1)——DataContext

    在WPF中,应用程序有两层:UI层和Data层.这里新建一个项目说明哪些是UI层,哪些是数据层. UI层很明显,就是用户看到的界面.但是数据层并不是下图所示: 上图中是UI层view的后台代码.当然, ...

  6. Prometheus Operator 自动发现和持久化

    Prometheus Operator 自动发现和持久化 之前在 Prometheus Operator 下面自定义一个监控选项,以及自定义报警规则的使用.那么我们还能够直接使用前面课程中的自动发现功 ...

  7. python selenium3 模拟点击+拖动+保存验证码 测试对象 58同城验证码

    #!/usr/bin/python # -*- coding: UTF-8 -*- # @Time : 2019/12/5 17:30 # @Author : shenghao/10347899@qq ...

  8. centos 中 Java环境变量配置

    一.安装java 1.搜索java包 yum search java 2.安装java包 -openjdk.x86_64 3.查看java安装目录 whereis java #找到Java目录 一般在 ...

  9. 一秒钟教会你如何 使用jfreechart制作图表,扇形图,柱形图,线型图,时序图,附上详细代码,直接看效果

    今天有小伙伴问到我怎么使用jfreeChat生成图标,去年就有一个这方便的的总结,今天再遇到,就总结出来,供大家参考: 第一个: 创建柱状图,效果图如下: 柱状图代码如下: package cn.xf ...

  10. 多进程之multiprocessing模块和进程池的实现

    转载:https://www.cnblogs.com/xiaobeibei26/p/6484849.html Python多进程之multiprocessing模块和进程池的实现 1.利用multip ...