Replace the string of the Chinese and American braces

Source: Internet
Author: User

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 ));}}
           
          
         
        
       
      
     
    
   
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.