Commit a5f112f4 by 龚榕城

模版替换方法

parent 33797db1
...@@ -635,7 +635,7 @@ public class PlatAlarmRecordServiceImpl extends ServiceImpl<PlatAlarmRecordMappe ...@@ -635,7 +635,7 @@ public class PlatAlarmRecordServiceImpl extends ServiceImpl<PlatAlarmRecordMappe
} }
public static String replaceParam(String oriContent,List<String> param) { public static String replaceParam(String oriContent,List<String> param) {
Pattern p = Pattern.compile("\\[#\\d+\\]|\\[#[\\p{IsHan}]+\\]|\\[#[^\\]]*\\]"); /*Pattern p = Pattern.compile("\\[#\\d+\\]|\\[#[\\p{IsHan}]+\\]|\\[#[^\\]]*\\]");
Matcher m = p.matcher(oriContent); Matcher m = p.matcher(oriContent);
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
if (!m.find()) { if (!m.find()) {
...@@ -652,6 +652,30 @@ public class PlatAlarmRecordServiceImpl extends ServiceImpl<PlatAlarmRecordMappe ...@@ -652,6 +652,30 @@ public class PlatAlarmRecordServiceImpl extends ServiceImpl<PlatAlarmRecordMappe
i++; i++;
} }
m.appendTail(sb); m.appendTail(sb);
return sb.toString();*/
Pattern pattern = Pattern.compile("\\[#(.*?)\\]");
Matcher matcher = pattern.matcher(oriContent);
int placeholderCount = 0;
while (matcher.find()) {
placeholderCount++;
}
if (param.size() > placeholderCount) {
param = new LinkedList<>(param).subList(param.size() - placeholderCount, param.size());
}
matcher.reset();
StringBuffer sb = new StringBuffer();
int i = 0;
while (matcher.find()) {
String replacement = i < param.size() ? param.get(i) : "";
matcher.appendReplacement(sb, replacement);
i++;
}
matcher.appendTail(sb);
return sb.toString(); return sb.toString();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment