Freemaker Replace函數(shù)的正則表達式運用
replace(param1,param2,param3)
param1 正則表達式;param2 將匹配的字符替換成指定字符;param3 模式
param3 參數(shù)如下
模式 | 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')}
輸出結(jié)果:
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元優(yōu)惠券
${str?replace('\\b\\d+積分','','r')}
輸出:兌換30元優(yōu)惠券
ps:freemarker的replace功能
替換字符串 replace
${s?replace(‘ba', ‘XY' )} ${s?replace(‘ba', ‘XY' , ‘規(guī)則參數(shù)')}
將s里的所有的ba替換成xy 規(guī)則參數(shù)包含: i r m s c f 具體含義如下:
· i: 大小寫不區(qū)分.
· f: 只替換第一個出現(xiàn)被替換字符串的字符串
· 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.
相關(guān)文章
SpringBoot?整合?Elasticsearch?實現(xiàn)海量級數(shù)據(jù)搜索功能
這篇文章主要介紹了SpringBoot?整合?Elasticsearch?實現(xiàn)海量級數(shù)據(jù)搜索,本文主要圍繞?SpringBoot?整合?ElasticSearch?接受數(shù)據(jù)的插入和搜索使用技巧,在實際的使用過程中,版本號尤其的重要,不同版本的?es,對應(yīng)的?api?是不一樣,需要的朋友可以參考下2022-07-07Java long 轉(zhuǎn)成 String的實現(xiàn)
這篇文章主要介紹了Java long 轉(zhuǎn)成 String的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09如何修改覆蓋spring boot默認日志策略logback詳解
這篇文章主要給大家介紹了關(guān)于如何修改覆蓋spring boot默認日志策略logback的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-10-10Springboot使用Security實現(xiàn)OAuth2授權(quán)驗證完整過程
安全管理是軟件系統(tǒng)必不可少的的功能。根據(jù)經(jīng)典的“墨菲定律”——凡是可能,總會發(fā)生。如果系統(tǒng)存在安全隱患,最終必然會出現(xiàn)問題,這篇文章主要介紹了SpringBoot使用Security實現(xiàn)OAuth2授權(quán)驗證完整過程2022-12-12Java設(shè)計模式之命令模式(Command模式)介紹
這篇文章主要介紹了Java設(shè)計模式之命令模式(Command模式)介紹,本文講解了Command模式的定義、如何使用命令模式等內(nèi)容,需要的朋友可以參考下2015-03-03Java數(shù)據(jù)結(jié)構(gòu)常見幾大排序梳理
Java常見的排序算法有:直接插入排序、希爾排序、選擇排序、冒泡排序、歸并排序、快速排序、堆排序等。本文詳解介紹它們的實現(xiàn)以及圖解,需要的可以參考一下2022-03-03