* 量詞: {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)
|
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) 就會只會做一次截取。