Freemaker Replace函數(shù)的正則表達(dá)式運(yùn)用
replace(param1,param2,param3)
param1 正則表達(dá)式;param2 將匹配的字符替換成指定字符;param3 模式
param3 參數(shù)如下
| 模式 | i | r | m | s | c | f |
| replace | 支持 | 支持 | 只和r 組合 | 只和r 組合 | 只和r 組合 | 支持 |
模式解釋?zhuān)?/strong>
i: Case insensitive: 忽略大小寫(xiě)
f: First only. That is, replace/find/etc. only the first occurrence of something.
r: The substring to find is a regular expression.標(biāo)準(zhǔn)正則表達(dá)式(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.在正則表達(dá)式中允許空格和注釋。
范例如下:
<#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: 大小寫(xiě)不區(qū)分.
· f: 只替換第一個(gè)出現(xiàn)被替換字符串的字符串
· r: XY是正則表達(dá)式
· 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)文章
詳談StringUtils3之StringUtils.isEmpty()和StringUtils.isB的區(qū)別
這篇文章主要介紹了詳談StringUtils3之StringUtils.isEmpty()和StringUtils.isB的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
Java?InputStream實(shí)戰(zhàn)之輕松讀取操作文件流
在Java中,輸入輸出是非常重要的基礎(chǔ)功能,其中,InputStream是Java中的一個(gè)重要輸入流類(lèi),用于從輸入源讀取數(shù)據(jù),下面我們就來(lái)學(xué)習(xí)一下InputStream類(lèi)的相關(guān)知識(shí)吧2023-10-10
基于Java實(shí)現(xiàn)五子棋小游戲(附源碼)
這篇文章主要為大家介紹了如何通過(guò)Java實(shí)現(xiàn)簡(jiǎn)單的五子棋游戲,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java游戲開(kāi)發(fā)有一定幫助,需要的可以參考一下2022-11-11
Java實(shí)現(xiàn)多線程輪流打印1-100的數(shù)字操作
這篇文章主要介紹了Java實(shí)現(xiàn)多線程輪流打印1-100的數(shù)字操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
java實(shí)現(xiàn)適用于安卓的文件下載線程類(lèi)
本文給大家分享的是java實(shí)現(xiàn)適用于安卓的文件下載線程類(lèi)的代碼,有需要的小伙伴可以參考下2015-07-07
SpringDataJpa like查詢(xún)無(wú)效的解決
這篇文章主要介紹了SpringDataJpa like查詢(xún)無(wú)效的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12

