Freemaker Replace函数的正则表达式运用
replace(param1,param2,param3)
param1正则表达式;param2将匹配的字符替换成指定字符;param3模式
param3参数如下
模式
i
r
m
s
c
f
replace
支持
支持
只和r
只和r
只和r
支持
模式解释:
i:Caseinsensitive:忽略大小写
f:Firstonly.Thatis,replace/find/etc.onlythefirstoccurrenceofsomething.
r:Thesubstringtofindisaregularexpression.标准正则表达式(http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html)
m:Multi-linemodeforregularexpressions.Inmulti-linemodetheexpressions^and$matchjustafterorjustbefore,respectively,alineterminatorortheendofthestring.Bydefaulttheseexpressionsonlymatchatthebeginningandtheendoftheentirestring.Notethat^and$doesn'tmatchtheline-breakcharacteritself.
s:Enablesdot-allmodeforregularexpressions(sameasPerlsinge-linemode).Indot-allmode,theexpression.matchesanycharacter,includingalineterminator.Bydefaultthisexpressiondoesnotmatchlineterminators.
c:Permitswhitespaceandcommentsinregularexpressions.在正则表达式中允许空格和注释。
范例如下:
<#assigns='foobArbaar'> ${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')}
输出结果:
foobArXYar
i:fooXYrXYar
if:fooXYrbaar
r:fooXYArXYr
ri:fooXYrXYr
rif:fooXYrbaar
更多范例:
原文:str=2积分兑换30元优惠券
${str?replace('\\b\\d+积分','','r')}
输出:兑换30元优惠券
ps:freemarker的replace功能
替换字符串replace
${s?replace(‘ba',‘XY')} ${s?replace(‘ba',‘XY',‘规则参数')}
将s里的所有的ba替换成xy规则参数包含:irmscf具体含义如下:
·i:大小写不区分.
·f:只替换第一个出现被替换字符串的字符串
·r: XY是正则表达式
·m:Multi-linemodeforregularexpressions.Inmulti-linemodetheexpressions^and$matchjustafterorjustbefore,respectively,alineterminatorortheendofthestring.Bydefaulttheseexpressionsonlymatchatthebeginningandtheendoftheentirestring.
·s:Enablesdotallmodeforregularexpressions(sameasPerlsinge-linemode).Indotallmode,theexpression.matchesanycharacter,includingalineterminator.Bydefaultthisexpressiondoesnotmatchlineterminators.
·c:Permitswhitespaceandcommentsinregularexpressions.