Regular gets the string for a particular identity and replaces

Source: Internet
Author: User

A regular string that gets a specific identity,

Pending string: #applyCom # the "#applyType #" item with the number "#applyNo #" has been requested for settles confirmation. Please log in to the system in time to view processing.

The #* #的形式作为占位符 is used here to identify the place to be processed.

Use regular processing code:

String content = "#applyCom # has requested settles confirmation for the" #applyType # "item with the number" #applyNo # ". Please log in to the system in time to view processing.   ";  Assemble the data that needs to be replaced, replace the placeholder hashmap<string with the values inside the map, string> map = new hashmap<string, string> ();  Map.put ("applycom", "a");  Map.put ("Applytype", "B");  Map.put ("Applyno", "C");  Pattern Pat = Pattern.compile ("(#[^#]*#)");//define regular Matcher mat = pat.matcher (content);  int i = 0; while (Mat.find ()) {//If there is a match String temp = mat.group (1). ToString (). substring (1, mat.group (1). ToString (). Length ()-1);//  Get placeholders, such as: #applyCom # will get applycom content = Content.replace (Mat.group (1), Map.get (temp)),//Get the value from the map, and replace the placeholder i++; } System.out.println (content);//Print final string

To get a string for a particular identity and replace it with a regular type

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.