正则式获取特定标识的字符串并替换
2015-11-18 09:24
423 查看
正则式获取特定标识的字符串,
待处理字符串:#applyCom# 已经对单号为“#applyNo#”的“#applyType#”事项申请办结确认。请及时登录系统查看处理。
这里使用#*#的形式作为占位符,标识是需要待处理的地方。
使用正则式处理代码:
待处理字符串:#applyCom# 已经对单号为“#applyNo#”的“#applyType#”事项申请办结确认。请及时登录系统查看处理。
这里使用#*#的形式作为占位符,标识是需要待处理的地方。
使用正则式处理代码:
String content = "#applyCom# 已经对单号为“#applyNo#”的“#applyType#”事项申请办结确认。请及时登录系统查看处理。"; //组装需要替换的数据,用map里面的值替换掉占位符 HashMap<String , String> map = new HashMap<String, String>(); map.put("applyCom", "a"); map.put("applyType", "b"); map.put("applyNo", "c"); Pattern pat = Pattern.compile("(#[^#]*#)");//定义正则式 Matcher mat = pat.matcher(content); int i = 0; while (mat.find()) {//如果有匹配 String temp = mat.group(1).toString().substring(1, mat.group(1).toString().length()-1);//获得占位符,如:#applyCom# 就会获得applyCom content = content.replace(mat.group(1), map.get(temp));//从map中获得值,并替换掉占位符 i++; } System.out.println(content);//打印最终字符串
相关文章推荐
- 数据库链接字符串查询网站
- IE:获取完整的网页
- Flex字符串比较 还有Flex字符串操作
- Ruby中创建字符串的一些技巧小结
- ASP下经常用的字符串等函数参考资料
- 将字符串小写转大写并延时输出的批处理代码
- 使用vbs获取雅虎汇率
- 将字符串转换成System.Drawing.Color类型的方法
- Lua源码中字符串类型的实现
- Lua性能优化技巧(四):关于字符串
- 字符串聚合函数(去除重复值)
- Ruby中的字符串编写示例
- ASP中让Replace替换不区分大小写的方法
- ReplaceSaveRemoteFile 替换、保存远程图片 的代码
- 总结的5个C#字符串操作方法分享
- sqlserver中求字符串中汉字的个数的sql语句
- sql server字符串非空判断实现方法
- 批处理BAT替换与截取字符串的用法t1=%a:~3%是什么意思
- 批量文件查找替换功能的vbs脚本
- VBS的字符串及日期操作相关函数