正規表達式符號速查表
Regular Expression Cheatsheet
發表於 ,約 5 分鐘閱讀 。
正規表達式符號速查表
Regular Expression Cheatsheet
正規表達式(Repgular Expression)是一個很好用的技能,雖然很常用,但有時候會一時忘記想要功能的符號是什麼,所以寫了篇速查用的整理筆記,幫助自己需要時可以回來查詢。尤其是最近有考慮幫助《世紀帝國 II》的隨機地圖腳本在本部落格呈現時,有程式碼高亮的風格,所以有在研究如何編寫 highlight.js 的語言擴充,就很常用到正規表達式,這也是寫這篇的主要原因。
請注意,本篇不是正規表達式的教學。
限制符
通常前面會接 1 字元(Character)或匹配模式(Pattern),這裡以 x 代稱。
<table>
<thead>
<tr>
<th>符號</th>
<th>說明</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>x?</code></td>
<td>符合 <code>x</code> 0 ~ 1 次</td>
</tr>
<tr>
<td><code>x*</code></td>
<td>符合 <code>x</code> 0 ~ N 次 [^1]</td>
</tr>
<tr>
<td><code>x+</code></td>
<td>符合 <code>x</code> 1 ~ N 次 [^1]</td>
</tr>
<tr>
<td><code>x{n}</code></td>
<td>確定符合 <code>x</code> n 次</td>
</tr>
<tr>
<td><code>x{n,}</code></td>
<td>至少符合 <code>x</code> n 次</td>
</tr>
<tr>
<td><code>x{n,m}</code></td>
<td>符合 <code>x</code> n ~ m 次</td>
</tr>
</tbody>
</table>
特殊符號
<table>
<thead>
<tr>
<th>符號</th>
<th>說明</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>^</code></td>
<td>符合輸入字串的開始位置,或是單行的開始處</td>
</tr>
<tr>
<td><code>$</code></td>
<td>符合輸入字串的結束位置,或是單行的結尾處</td>
</tr>
<tr>
<td><code>?</code></td>
<td>非貪婪模式搜尋 [^2]</td>
</tr>
<tr>
<td><code>\b</code></td>
<td>符合一個單詞邊界 [^3]</td>
</tr>
<tr>
<td><code>\B</code></td>
<td>符合非單詞邊界</td>
</tr>
</tbody>
</table>
匹配模式
這邊以 x、y 代稱 1 字元(Character)或匹配模式(Pattern)
<table>
<thead>
<tr>
<th>符號</th>
<th>說明</th>
<th>紀錄</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>(x)</code></td>
<td>符合 x</td>
<td>是</td>
</tr>
<tr>
<td><code>(?:x)</code></td>
<td>符合 x</td>
<td>否</td>
</tr>
<tr>
<td><code>x(?=y)</code></td>
<td>正向肯定預查,符合後面接 <code>y</code> 的 <code>x</code></td>
<td>否</td>
</tr>
<tr>
<td><code>x(?!y)</code></td>
<td>正向否定預查,符合後面不接 <code>y</code> 的 <code>x</code></td>
<td>否</td>
</tr>
<tr>
<td><code>(?<=x)y</code></td>
<td>反向肯定預查,符合前面接 <code>x</code> 的 <code>y</code></td>
<td>否</td>
</tr>
<tr>
<td><code>(?<!x)y</code></td>
<td>反向否定預查,符合後面不接 <code>x</code> 的 <code>y</code></td>
<td>否</td>
</tr>
</tbody>
</table>
自訂字元集合
這邊以 x、y 代稱 1 字元(Character)或匹配模式(Pattern)
<table>
<thead>
<tr>
<th>符號</th>
<th>說明</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>x|y</code></td>
<td>符合 <code>x</code> 或 <code>y</code></td>
</tr>
<tr>
<td><code>[abc]</code></td>
<td>字元集合</td>
</tr>
<tr>
<td><code>[^abc]</code></td>
<td>排除型字元集合</td>
</tr>
<tr>
<td><code>[a-z]</code></td>
<td>字元範圍</td>
</tr>
<tr>
<td><code>[^a-z]</code></td>
<td>排除型字元範圍。</td>
</tr>
</tbody>
</table>
內建可視字元集合
<table>
<thead>
<tr>
<th>符號</th>
<th>說明</th>
<th>等價</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>.</code></td>
<td>除了 <code>\n</code> 以外的任何單字</td>
<td><code>[^\n]</code></td>
</tr>
<tr>
<td><code>\d</code></td>
<td>數字字元</td>
<td><code>[0-9]</code></td>
</tr>
<tr>
<td><code>\D</code></td>
<td>非數字字元</td>
<td><code>[^0-9]</code></td>
</tr>
<tr>
<td><code>\w</code></td>
<td>包括底線的單詞字元</td>
<td><code>[A-Za-z0-9_]</code></td>
</tr>
<tr>
<td><code>\W</code></td>
<td>任何非單詞字元</td>
<td><code>[^A-Za-z0-9_]</code></td>
</tr>
</tbody>
</table>
內建空白字元集合
<table>
<thead>
<tr>
<th>符號</th>
<th>說明</th>
<th>等價</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>\s</code></td>
<td>任何空白字元</td>
<td><code>[ \f\n\r\t\v]</code></td>
</tr>
<tr>
<td><code>\S</code></td>
<td>任何非空白字元</td>
<td><code>[^ \f\n\r\t\v]</code></td>
</tr>
<tr>
<td><code>\f</code></td>
<td>符合一個換頁符</td>
<td><code>[\x0c\cL]</code></td>
</tr>
<tr>
<td><code>\n</code></td>
<td>符合一個換行符</td>
<td><code>[\x0a\cJ]</code></td>
</tr>
<tr>
<td><code>\r</code></td>
<td>符合一個 Enter 符</td>
<td><code>[\x0d\cM]</code></td>
</tr>
<tr>
<td><code>\t</code></td>
<td>符合一個制表符</td>
<td><code>[\x09\cI]</code></td>
</tr>
<tr>
<td><code>\v</code></td>
<td>符合一個垂直制表符</td>
<td><code>[\x0b\cK]</code></td>
</tr>
</tbody>
</table>
特殊字元
<table>
<thead>
<tr>
<th>符號</th>
<th>說明</th>
<th>等價</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>\</code></td>
<td>轉譯字元</td>
<td></td>
</tr>
<tr>
<td><code>\cX</code></td>
<td>控制字元,X 為 A-Z。也就是 Ctrl + [A-Z] 的意思</td>
<td></td>
</tr>
</tbody>
</table>
- 這邊的 N 表示為自然數、正整數。
- 非貪婪模式儘可能少的符合所搜尋的字串。
- 也就是指單詞和空格間的位置