summaryrefslogtreecommitdiffhomepage
path: root/zh-tw/pcre-tw.html.markdown
diff options
context:
space:
mode:
authorHinet60613 <hinet60613@gmail.com>2017-11-15 21:57:30 +0800
committerHinet60613 <hinet60613@gmail.com>2017-11-15 21:57:30 +0800
commit0e0465218542ab81dbdde347d75aab5f907b4577 (patch)
tree1f20e568dfc60d0086c2d69b54976b8b0cd837b9 /zh-tw/pcre-tw.html.markdown
parent990f51293b1e85bc1146b291784474c82fabba37 (diff)
Create PCRE-tw.
Diffstat (limited to 'zh-tw/pcre-tw.html.markdown')
-rw-r--r--zh-tw/pcre-tw.html.markdown84
1 files changed, 84 insertions, 0 deletions
diff --git a/zh-tw/pcre-tw.html.markdown b/zh-tw/pcre-tw.html.markdown
new file mode 100644
index 00000000..01870eaf
--- /dev/null
+++ b/zh-tw/pcre-tw.html.markdown
@@ -0,0 +1,84 @@
+---
+language: PCRE
+filename: pcre.txt
+contributors:
+ - ["Sachin Divekar", "http://github.com/ssd532"]
+translators:
+ - ["Michael Yeh", "https://hinet60613.github.io/"]
+filename: pcre-tw.py
+lang: zh-tw
+---
+
+正規表達式(regular expression,或縮寫為regex, regexp)是一種用來表示搜尋模式的特殊字串。例如,你可以用`/^[a-z]+:/`來從網址`http://github.com`中擷取出`http:`這段http協定名稱。
+
+相容Perl正規表達式(Perl Compatible Regular Expressions, PCRE)是一個實作正規表達式的C語言函式庫。此函式庫在1997年被開發出來,在當時面對複雜字串處理時大多會選擇使用Perl。也因為如此,PCRE大多的正規表達式語法都很酷似Perl。PCRE語法被廣泛運用在許多大專案中,包括PHP、Apache、R等。
+
+PCRE中的超字元(metacharacter)主要可以分為以下兩類:
+* 在中括號外會被辨識的字元
+```
+ \ 通用跳脫字元
+ ^ 字串開頭 或 行首
+ $ 字串結尾 或 行尾
+ . 除了換行符號外的任何字元
+ [ 字元集合定義開始
+ | 支流開始
+ ( 子串模式定義開始
+ ) 子串模式定義結束
+ ? extends the meaning of (
+ 同時為數量0或1配對
+ also quantifier minimizer
+ * 量詞 至少0個 至多無限個
+ + 量詞 至少1個 至多無限個
+ 同時為佔有型量詞
+ { 最大/最小量詞開始
+```
+
+* 在中括號內會被辨識的超字元,在中括號外會被視為字元集合使用
+
+```
+
+ \ 通用跳脫字元
+ ^ 非字元集合的字,但只會抓到第一個符合的字元
+ - 字元範圍
+ [ POSIX字元集合(若後面接POSIX格式)
+ ] 字元集合定義結束
+
+```
+
+PCRE提供了一些通用的字元類型,可被當作字元集合使用
+```
+ \d 任何數字字元
+ \D 任何非數字字元
+ \h 任何水平空白字元
+ \H 任何非水平空白字元
+ \s 任何空白字元
+ \S 任何非空白至元
+ \v 任何垂直空白字元
+ \V 任何非垂直空白字元
+ \w 任何英文字
+ \W 任何非英文字
+```
+
+## 範例
+
+我們以字串 `66.249.64.13 - - [18/Sep/2004:11:07:48 +1000] "GET /robots.txt HTTP/1.0" 200 468 "-" "Googlebot/2.1"` 作為範例,這是一個標準的Apache存取記錄。
+
+| 正規表達式 | 結果 | 說明 |
+| :---- | :-------------- | :------ |
+| GET | GET | GET 抓取 GET 字串 (會分別大小寫) |
+| \d+.\d+.\d+.\d+ | 66.249.64.13 | `\d+` 抓取數字字元,數量由 `+` 定義為至少一個至多無限個。 `\.` 抓取 `.` 字元 |
+| (\d+\.){3}\d+ | 66.249.64.13 | `(\d+\.){3}` 會試著抓取剛好三次的 (`\d+\.`) |
+| \[.+\] | [18/Sep/2004:11:07:48 +1000] | `.+` 抓取除了換行符號以外的任何字元, `.` 表示任意字元 |
+| ^\S+ | 66.249.64.13 | `^` 為行首, `\S+` 抓取至少一個非空白字元 |
+| \+[0-9]+ | +1000 | `\+` 抓取 `+` 字元。 `[0-9]` 字元集表示剛好一個數字字元。 可以用 `\+\d+` 達到相同效果。 |
+
+以上範例皆可在 https://regex101.com/ 測試,步驟如下:
+
+1. 複製範例字串到 `TEST STRING` 區域
+2. 複製正規表達式字串到 `Regular Expression` 區域
+3. 網頁會顯示自動表達式抓取結果
+
+
+## 更多資料
+
+