package com.infosec.ztpdp.policycenter.module.policycenter.scene.controller; import java.text.DateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Pattern; import com.github.xiaoymin.knife4j.core.util.CollectionUtils; import com.infosec.ztpdp.policycenter.common.sms.MailAndSmsUtils; public class text { /** * 时间. */ private static final Pattern DATE = Pattern.compile("\\$DATE", Pattern.CASE_INSENSITIVE); /** * 下划线. */ private static final Pattern NEWLINE = Pattern.compile("\\$NL", Pattern.CASE_INSENSITIVE); public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(16); map.put("IP", "1.1.1.1"); map.put("CPU", "30"); map.put("DISK", "90"); map.put("MEMORY", "70"); String returnval = "您好,您IP为[$IP]的设备,磁盘使用率已达到[$DISK%],内存使用率已达到[$MEMORY%],CPU使用率已达到[$CPU%]"; try { MailAndSmsUtils mailUtils = new MailAndSmsUtils(); List<String> list = mailUtils.getPromptInfo(); if (!CollectionUtils.isEmpty(list)) { for (String info : list) { String fieldVal = ""; if (map.get(info) != null) { fieldVal = map.get(info).toString(); } //info:为要替换的字符 fieldVal:替换后的值 returnval = Pattern.compile(("\\$" + info), Pattern.CANON_EQ).matcher(returnval) .replaceAll((String) fieldVal); } } } catch (Exception e) { e.printStackTrace(); } String currentdate = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(new Date()); String newline = System.getProperty("line.separator"); returnval = DATE.matcher(returnval).replaceAll(currentdate); returnval = NEWLINE.matcher(returnval).replaceAll(newline); newline = null; currentdate = null; System.out.println(returnval); } }
标签:map,String,Pattern,util,内容,returnval,字符串,import,替换 From: https://www.cnblogs.com/cuijinlong/p/17769380.html