Use the regular expression of the Freemaker Replace function.

Source: Internet
Author: User

Use the regular expression of the Freemaker Replace function.

Replace (param1, param2, param3)

Param1 regular expression; param2 replaces matched characters with specified characters; param3 Mode

Param3 parameters:

Mode I R M S C F
Replace Supported Supported Only combined with r Only combined with r Only combined with r Supported

 

 

Mode description:

I: Case insensitive

F: First only. That is, replace/find/etc. only the first occurrence of something.

R: The substring to find is a regular expression. Standard regular expression (http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html)

M: Multi-line mode for regular expressions. in multi-line mode the expressions ^ and $ match just after or just before, respectively, a line terminator or the end of the string. by default these expressions only match at the beginning and the end of the entire string. note that ^ and $ doesn' t match the line-break character itself.

S: Enables dot-all mode for regular expressions (same as Perl singe-line mode ). in dot-all mode, the expression. matches any character, including a line terminator. by default this expression does not match line terminators.

C: Permits whitespace and comments in regular expressions. Spaces and comments are allowed in regular expressions.

Example:

<#assign s = 'foo bAr baar'>${s?replace('ba', 'XY')}i: ${s?replace('ba', 'XY', 'i')}if: ${s?replace('ba', 'XY', 'if')}r: ${s?replace('ba*', 'XY', 'r')}ri: ${s?replace('ba*', 'XY', 'ri')}rif: ${s?replace('ba*', 'XY', 'rif')}  

Output result:

foo bAr XYari: foo XYr XYarif: foo XYr baarr: foo XYAr XYrri: foo XYr XYrrif: foo XYr baar 

More examples:

Original article: str = 2 points for 30 yuan coupon $ {str? Replace ('\ B \ d + credit', ', 'R')} output: redeem a 30 yuan coupon

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.