Java Regular expression parsing SMS template

Source: Internet
Author: User

/** *  */ PackageTestjava.java;ImportJava.util.HashMap;ImportJava.util.Map;ImportJava.util.Scanner;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;/** * @authorXXX.SJTU * @function * @date April 12, 2016 *@version */ Public classReplacetest {/**     * @paramargs*/     Public Static voidMain (string[] args) {String tempaltecontent= "your" $CarBrand $ "(license plate $carplatenum$) has been leased (order number: $orderNo $). This rental income $rentincome$ yuan, $ServiceExpense $ $ServiceExpenseProxy $ $You 2renteradjust$ $Renter 2youadjust$ $HardwareDeposit $ $OWNERTRANSDEBT $ $TotalIncome $ after 1 business days to settle to the bank card you set up. You can view the details on the app owner Benefits page.; Map Contentparammap=NewHashMap (); Contentparammap.put ("Carbrand", "Audi"); Contentparammap.put ("Carplatenum", "Shanghai A0001"); Contentparammap.put ("OrderNo", "1245221158"); Contentparammap.put ("Rentincome", "2000"); Contentparammap.put ("Serviceexpense", "Platform service charge 100 yuan,"); Scanner SC=NewScanner (tempaltecontent); StringBuffer buf=NewStringBuffer (); Try{Pattern P= Pattern.compile ("[$]" ([^$]*?) [$]");  while(Sc.hasnext ()) {System.out.println (sc.tostring ()); Matcher m=P.matcher (Sc.nextline ());  while(M.find ()) {//Find and Replace parametersSystem.out.println (M.group (1)); //get the value from the map based on keyM.appendreplacement (buf, Contentparammap.get (M.group (1))! =NULL? Contentparammap.get (M.group (1)). ToString (): "");            } m.appendtail (BUF); }//if (Stringutils.isempty (buf.tostring ())) {//buf.append (tempaltecontent);//            }}Catch(Exception e) {//logger.error ("Replace SMS template content Error! ", e);E.printstacktrace (); }finally{sc.close (); } System.out.println ("Buf=" +buf); }}

Java Regular expression parsing SMS template

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.