# 正規表達式<br>Regular Expression
----
## 為什麼要學?
超方便!
未來一定會用到的字串處理技能
----
## 推薦學習資源
**[RegexOne](https://regexone.com/)**
----
## Let's GO!
---
| 語法 | 意義 |
| :-: | :-: |
| a-z | 字元 a 到 z |
| A-Z | 字元 A 到 Z |
| 0-9 | 字元 0 到 9 |
----
| 語法 | 意義 |
| :-: | :-: |
| [abc] | 匹配 $a$ 或 $b$ 或 $c$ 字元 |
| [^abc ]| 匹配**不含** $a、b、c$ 的字元
| {m} | 重複 $m$ 次 |
| {m, n} | 重複 $m$ 次或 $m+1$ 次或$... n$次|
| * | 重複 $0$ 次以上 |
| + | 重複 $1$ 次以上
----
| 語法 | 意義 |
| :-: | :-: |
| ? | 可選字元 |
| . | 任意字元 |
| \\. | . |
| \d | [0-9] |
| \w | [A-Za-z0-9] |
| \s | 空白字元 |
----
| 語法 | 意義 |
| :-: | :-: |
| ^...$ | 開始和結束 |
| (abc) | abc 為群組 |
| (a(bc)) | bc 為子群組 |
| ( abc \| def)| 匹配 abc 或 def 字串|
---
## 練習
(建議至 RegexOne 上練習效果更佳)
以下提供幾種範例解,供讀者快速理解用。
----
Match abcdefg
Match abcde
Match abc
`Ans: "[a-z]+"、"\w+"`
----
Match abc123xyz
Match var g = 123;
Match define "123"
`Ans: "[a-z0-9 =";]+"、"[\w" =;]+"`
----
Match cat.
Match 896.
Match ?=+.
Skip abc1
`Ans: "...\."`
----
Match can
Match man
Match fan
Skip dan
Skip ran
Skip pan
`Ans: "[cmf]an"、"[^drp]an"`
----
Match wazzzzzup
Match wazzzup
Skip wazup
`Ans: "waz{2,}up"、"waz{3,5}up"`
----
Match aaaabcc
Match aabbbbc
Match aacc
Skip a
`Ans: "a{2,4}b{0,4}c{1,2}"、"aa+b*c+"`
----
Match 1 file found?
Match 2 files found?
Match 24 files found?
Skip No files found.
`Ans: "\d+ files? found\?"`
----
Match Mission: successful
Skip Last Mission: unsuccessful
Skip Next Mission: successful upon capture of target
`Ans: "^Mission: successful$"`
----
Match I love cats
Match I love dogs
Skip I love logs
Skip I love cogs
`Ans: "I love (cats|dogs)"`
{"metaMigratedAt":"2023-06-17T02:02:57.277Z","metaMigratedFrom":"Content","title":"正規表達式<br>Regular Expression","breaks":true,"contributors":"[{\"id\":\"6959a7e1-d7a4-4bd9-a737-406a901b746c\",\"add\":1919,\"del\":297}]"}