Import java. util. arrayList; import java. util. linkedHashMap; import java. util. list; import java. util. map; import java. util. regex. matcher; import java. util. regex. pattern;/*** creation date: June 9, 2014 17:23:29 * Author: Huang Fei * contact information: QQ: 834865081 * Email: 834865081@qq.com **/public class RegUtil {/*** @ author * is used to find the characters in the middle of all dollar braces in a string * @ param value string **/public static List
FindData (String value) {/* String matching the dollar braces */String reg = "\\\ \\{ ([^}] + )\\}"; pattern p = Pattern. compile (reg); Matcher matcher = p. matcher (value); ArrayList
Array = new ArrayList
();/* Find the string of all dollar braces */while (matcher. find () {String tempValue = matcher. group ();/* remove the character in the braces */tempValue = tempValue. substring (2, tempValue. length ()-1);/* determines whether the character has been found */if (! Array. contains (tempValue) {array. add (tempValue) ;}} return array ;} /*** @ author Huang Fei * replace the dollar String * @ param value String * map replacement value * noExistsReplace all replace **/public static String replace (String value, Map
Map, boolean noExistsReplace) {for (Map. Entry
Entry: map. entrySet () {String key = "\\\ \{" + entry. getKey () + "\\}"; value = value. replaceAll (key, entry. getValue ();} if (noExistsReplace) {List
Datas = findData (value); Map
TempValue = new LinkedHashMap
(); For (String data: datas) {tempValue. put (data, "") ;}return replace (value, tempValue, false) ;}return value ;}public static void main (String [] args) {String value = "$ {zs} Very good, $ {li}"; List
Datas = findData (value); Map
Map = new LinkedHashMap
(); Int idx = 1; for (String data: datas) {map. put (data, "value" + (++ idx);} map. remove ("li"); System. out. println (replace (value, map, true ));}}