zoukankan      html  css  js  c++  java
  • 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元优惠券

    更多信息可以参考:

    http://freemarker.org/docs/ref_builtins_string.html#ref_builtin_replace

  • 相关阅读:
    8.13实习报告
    8.10实习报告
    8.9实习报告
    8.8实习报告
    8.7实习报告
    关于线索二叉树建立和遍历
    main函数的位置可以任意
    返回指针值的函数和函数指针的区别
    runtime error: store to address 0x625000002048 with insufficient space for an object of type 'double' (solution.c) 0x625000002048: note: pointer points here
    m=-n++
  • 原文地址:https://www.cnblogs.com/xiaoyangjia/p/4173994.html
Copyright © 2011-2022 走看看