. :anyone
\d :any single digital
[^/]+ :1 or >1 until /, / not included
[^abc]+ :1 or >1 until "a" or "b" or "c", "a" or "b" or "c" not included
+ :1 or >1
? :0 or 1
* :0 or any
\d{1,3} :1 or 2 or 3 digital(s)
match :(pattern)
to use content inside() with:\1
class.now_class='201' ==> class.now_class='{201}'
ex: find:(\d{3}) ==> replace:{\1}
referce : http://www.regular-expressions.info/lookaround.html
look-around:lookahead & lookbehind
look-ahead: ^((?=pattern).)*$ ==> 包含pattern則為true
^(target(?=pattern).)*$ ==> 包含target+pattern則為true
^((?!pattern).)*$ ==> 不包含pattern則為true
^(target(?!pattern).)*$ ==> 不包含target+pattern則為true
look-behind: ^(.(?<=pattern))*$ ==> 包含pattern則為true
^(.(?<=pattern)target)*$ ==> 包含pattern+target則為true
^(.(?<!pattern))*$ ==> 不包含pattern則為true
^(.(?!pattern)target)*$ ==> 不包含pattern+target則為true
456x56
ex: find:(?=(\d+)\u\1) ==> find 56x56 的開頭起始位置,使用取代會更容易看出效果
比對原則
在通用式所採用的比對原則是「最大比對(Maximal Match)或是」「貪心比對」(Greedy Match),因此會盡量「貪」到越多的字元越好。
若使用通用式 'foo.*bar' 來比對字串 'The food is under the bar in the barn.',所比對到的是長字串 ‘food is under the bar in the bar’,而不是另一個符合比對標準的短字串 ‘food is under the bar’。
若要使通用式進行極小比對(Minimal Match),也就是在符合比對的條件下,選擇最短的字串,那麼就要在星號之後加上問號。
條件A(...pattern..)條件B 取代成 ($1) ==> ($1)的部分會被 (..pattern..) 取代
本例原文
11
學術群
26
商業與管理群
本例取代結果
11學術群
26商業與管理群
如果取代的結果含有(或), 要用跳脫字元標記
在padmin裡面
regexp_replace(birthday, '(pattern)' , '\\1', 'g')
符合的目標會在\\1出現
non-greedy 選擇取代