Freemaker Replace函数的正则表达式运用
replace(param1,param2,param3)
param1 正则表达式;param2 将匹配的字符替换成指定字符;param3 模式
param3 参数如下
模式 | i | r | m | s | c | f |
replace | 支持 | 支持 | 只和r 组合 | 只和r 组合 | 只和r 组合 | 支持 |
模式解释:
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.标准正则表达式(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.在正则表达式中允许空格和注释。
范例如下:
<#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')}
输出结果:
foo bAr XYar
i: foo XYr XYar
if: foo XYr baar
r: foo XYAr XYr
ri: foo XYr XYr
rif: foo XYr baar
更多范例:
原文:str = 2积分兑换30元优惠券
${str?replace('\\b\\d+积分','','r')}
输出:兑换30元优惠券
ps:freemarker的replace功能
替换字符串 replace
${s?replace(‘ba', ‘XY' )} ${s?replace(‘ba', ‘XY' , ‘规则参数')}
将s里的所有的ba替换成xy 规则参数包含: i r m s c f 具体含义如下:
· i: 大小写不区分.
· f: 只替换第一个出现被替换字符串的字符串
· r: XY是正则表达式
· 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.
· s: Enables dotall mode for regular expressions (same as Perl singe-line mode). In dotall 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.
相关文章
SpringBoot 整合 Elasticsearch 实现海量级数据搜索功能
这篇文章主要介绍了SpringBoot 整合 Elasticsearch 实现海量级数据搜索,本文主要围绕 SpringBoot 整合 ElasticSearch 接受数据的插入和搜索使用技巧,在实际的使用过程中,版本号尤其的重要,不同版本的 es,对应的 api 是不一样,需要的朋友可以参考下2022-07-07如何修改覆盖spring boot默认日志策略logback详解
这篇文章主要给大家介绍了关于如何修改覆盖spring boot默认日志策略logback的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2018-10-10Springboot使用Security实现OAuth2授权验证完整过程
安全管理是软件系统必不可少的的功能。根据经典的“墨菲定律”——凡是可能,总会发生。如果系统存在安全隐患,最终必然会出现问题,这篇文章主要介绍了SpringBoot使用Security实现OAuth2授权验证完整过程2022-12-12
最新评论