正規表達式學習

*  量詞: {n,m}, *, +, ?

* regex 的比對是貪多的比對,也就是會盡可能的最大比對,若要進行不貪多的比對,可以在量詞後面加上?, ex: \d+? --> 原來 \d+ 比對 136 會比對到 136,而 \d+? 則會比對到 3  個 1, 3, 6

* VIM 的不貪多(:h non-greedy) 寫法比較不同, 參考

   Greedy (貪多)量詞:

Quantifier
Description
*
matches 0 or more of the preceding characters, ranges or metacharacters .* matches everything including empty line
\+
matches 1 or more of the preceding characters...
\=
matches 0 or 1 more of the preceding characters...
\{n,m}
matches from n to m of the preceding characters...
\{n}
matches exactly n times of the preceding characters...
\{,m}
matches at most m (from 0 to m) of the preceding characters...
\{n,}
matches at least n of of the preceding characters...
where  n and  m are positive integers (>0)
   non-Greedy (不貪多)量詞:

Quantifier
Description
\{-}
matches 0 or more of the preceding atom, as few as possible
\{-n,m}
matches 1 or more of the preceding characters...
\{-n,}
matches at lease or more of the preceding characters...
\{-,m}
matches 1 or more of the preceding characters...
where  n and  m are positive integers (>0)



* () 括弧有分組與截取兩種涵義;ex: (aircraft | airplane | jet) 也等同 (air(craft|plane)|jet) ==>但第二種寫法比對到 aircraft時會做兩次截取。第一次是 aircraft,第二次是 craft;

* 承上,若要取消截取可以在 '(' 後面加上 ":?"  也就是 (air(:?craft|plane)|jet) 就會只會做一次截取。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值