正則表達式完整入門教程,含在線練習

什麼是正則表達式?

正則表達式是一組由字母和符號組成的特殊文本,它可以用來從文本中找出滿足你想要的格式的句子。

一個正則表達式是一種從左到右匹配主體字符串的模式。
「Regular expression」這個詞比較拗口,我們常使用縮寫的術語「regex」或「regexp」。
正則表達式可以從一個基礎字符串中根據一定的匹配模式替換文本中的字符串、驗證表單、提取字符串等等。

想像你正在寫一個應用,然後你想設定一個用戶命名的規則,讓用戶名包含字符、數字、下劃線和連字符,以及限制字符的個數,好讓名字看起來沒那麼丑。
我們使用以下正則表達式來驗證一個用戶名:

以上的正則表達式可以接受 john_doejo-hn_doejohn12_as
但不匹配Jo,因為它包含了大寫的字母而且太短了。

目錄

1. 基本匹配

正則表達式其實就是在執行搜索時的格式,它由一些字母和數字組合而成。
例如:一個正則表達式 the,它表示一個規則:由字母t開始,接着是h,再接着是e

"the" => The fat cat sat on the mat.
"the" => The fat cat sat on the mat.

在線練習

正則表達式123匹配字符串123。它逐個字符的與輸入的正則表達式做比較。

正則表達式是大小寫敏感的,所以The不會匹配the

"The" => The fat cat sat on the mat.
"The" => The fat cat sat on the mat.

在線練習

2. 元字符

正則表達式主要依賴於元字符。
元字符不代表他們本身的字面意思,他們都有特殊的含義。一些元字符寫在方括號中的時候有一些特殊的意思。以下是一些元字符的介紹:

元字符 描述
. 句號匹配任意單個字符除了換行符。
[ ] 字符種類。匹配方括號內的任意字符。
[^ ] 否定的字符種類。匹配除了方括號里的任意字符
* 匹配>=0個重複的在*號之前的字符。
+ 匹配>=1個重複的+號前的字符。
? 標記?之前的字符為可選.
{n,m} 匹配num個大括號之前的字符或字符集 (n <= num <= m).
(xyz) 字符集,匹配與 xyz 完全相等的字符串.
| 或運算符,匹配符號前或後的字符.
\ 轉義字符,用於匹配一些保留的字符 [ ] ( ) { } . * + ? ^ $ \ |
^ 從開始行開始匹配.
$ 從末端開始匹配.

2.1 點運算符 .

.是元字符中最簡單的例子。
.匹配任意單個字符,但不匹配換行符。
例如,表達式.ar匹配一個任意字符後面跟着是ar的字符串。

".ar" => The car parked in the garage.
".ar" => The car parked in the garage.

在線練習

2.2 字符集

字符集也叫做字符類。
方括號用來指定一個字符集。
在方括號中使用連字符來指定字符集的範圍。
在方括號中的字符集不關心順序。
例如,表達式[Tt]he 匹配 theThe

"[Tt]he" => The car parked in the garage.
"[Tt]he" => The car parked in the garage.

在線練習

方括號的句號就表示句號。
表達式 ar[.] 匹配 ar.字符串

"ar[.]" => A garage is a good place to park a car.
"ar[.]" => A garage is a good place to park a car.

在線練習

2.2.1 否定字符集

一般來說 ^ 表示一個字符串的開頭,但它用在一個方括號的開頭的時候,它表示這個字符集是否定的。
例如,表達式[^c]ar 匹配一個後面跟着ar的除了c的任意字符。

"[^c]ar" => The car parked in the garage.
"[^c]ar" => The car parked in the garage.

在線練習

2.3 重複次數

後面跟着元字符 +* or ? 的,用來指定匹配子模式的次數。
這些元字符在不同的情況下有着不同的意思。

2.3.1 *

*號匹配 在*之前的字符出現大於等於0次。
例如,表達式 a* 匹配0或更多個以a開頭的字符。表達式[a-z]* 匹配一個行中所有以小寫字母開頭的字符串。

"[a-z]*" => The car parked in the garage #21.
"[a-z]*" => The car parked in the garage #21.

在線練習

*字符和.字符搭配可以匹配所有的字符.*
*和表示匹配空格的符號\s連起來用,如表達式\s*cat\s*匹配0或更多個空格開頭和0或更多個空格結尾的cat字符串。

"\s*cat\s*" => The fat cat sat on the concatenation.
"\s*cat\s*" => The fat cat sat on the concatenation.

在線練習

2.3.2 +

+號匹配+號之前的字符出現 >=1 次。
例如表達式c.+t 匹配以首字母c開頭以t結尾,中間跟着至少一個字符的字符串。

"c.+t" => The fat cat sat on the mat.
"c.+t" => The fat cat sat on the mat.

在線練習

2.3.3 ?

在正則表達式中元字符 ? 標記在符號前面的字符為可選,即出現 0 或 1 次。
例如,表達式 [T]?he 匹配字符串 heThe

"[T]he" => The car is parked in the garage.
"[T]he" => The car is parked in the garage.

在線練習

"[T]?he" => The car is parked in the garage.
"[T]?he" => The car is parked in the garage.

在線練習

2.4 {}

在正則表達式中 {} 是一個量詞,常用來限定一個或一組字符可以重複出現的次數。
例如, 表達式 [0-9]{2,3} 匹配最少 2 位最多 3 位 0~9 的數字。

"[0-9]{2,3}" => The number was 9.9997 but we rounded it off to 10.0.
"[0-9]{2,3}" => The number was 9.9997 but we rounded it off to 10.0.

在線練習

我們可以省略第二個參數。
例如,[0-9]{2,} 匹配至少兩位 0~9 的數字。

"[0-9]{2,}" => The number was 9.9997 but we rounded it off to 10.0.
"[0-9]{2,}" => The number was 9.9997 but we rounded it off to 10.0.

在線練習

如果逗號也省略掉則表示重複固定的次數。
例如,[0-9]{3} 匹配3位數字

"[0-9]{3}" => The number was 9.9997 but we rounded it off to 10.0.
"[0-9]{3}" => The number was 9.9997 but we rounded it off to 10.0.

在線練習

2.5 (...) 特徵標群

特徵標群是一組寫在 (...) 中的子模式。(...) 中包含的內容將會被看成一個整體,和數學中小括號( )的作用相同。例如, 表達式 (ab)* 匹配連續出現 0 或更多個 ab。如果沒有使用 (...) ,那麼表達式 ab* 將匹配連續出現 0 或更多個 b 。再比如之前說的 {} 是用來表示前面一個字符出現指定次數。但如果在 {} 前加上特徵標群 (...) 則表示整個標群內的字符重複 N 次。

我們還可以在 () 中用或字符 | 表示或。例如,(c|g|p)ar 匹配 cargarpar.

"(c|g|p)ar" => The car is parked in the garage.
"(c|g|p)ar" => The car is parked in the garage.

在線練習

2.6 | 或運算符

或運算符就表示或,用作判斷條件。

例如 (T|t)he|car 匹配 (T|t)hecar

"(T|t)he|car" => The car is parked in the garage.
"(T|t)he|car" => The car is parked in the garage.

在線練習

2.7 轉碼特殊字符

反斜線 \ 在表達式中用於轉碼緊跟其後的字符。用於指定 { } [ ] / \ + * . $ ^ | ? 這些特殊字符。如果想要匹配這些特殊字符則要在其前面加上反斜線 \

例如 . 是用來匹配除換行符外的所有字符的。如果想要匹配句子中的 . 則要寫成 \. 以下這個例子 \.?是選擇性匹配.

"(f|c|m)at\.?" => The fat cat sat on the mat.
"(f|c|m)at\.?" => The fat cat sat on the mat.

在線練習

2.8 錨點

在正則表達式中,想要匹配指定開頭或結尾的字符串就要使用到錨點。^ 指定開頭,$ 指定結尾。

2.8.1 ^

^ 用來檢查匹配的字符串是否在所匹配字符串的開頭。

例如,在 abc 中使用表達式 ^a 會得到結果 a。但如果使用 ^b 將匹配不到任何結果。因為在字符串 abc 中並不是以 b 開頭。

例如,^(T|t)he 匹配以 Thethe 開頭的字符串。

"(T|t)he" => The car is parked in the garage.
"(T|t)he" => The car is parked in the garage.

在線練習

"^(T|t)he" => The car is parked in the garage.
"^(T|t)he" => The car is parked in the garage.

在線練習

2.8.2 $

同理於 ^ 號,$ 號用來匹配字符是否是最後一個。

例如,(at\.)$ 匹配以 at. 結尾的字符串。

"(at\.)" => The fat cat. sat. on the mat.
"(at\.)" => The fat cat. sat. on the mat.

在線練習

"(at\.)$" => The fat cat. sat. on the mat.
"(at\.)$" => The fat cat. sat. on the mat.

在線練習

3. 簡寫字符集

正則表達式提供一些常用的字符集簡寫。如下:

簡寫 描述
. 除換行符外的所有字符
\w 匹配所有字母數字,等同於 [a-zA-Z0-9_]
\W 匹配所有非字母數字,即符號,等同於: [^\w]
\d 匹配數字: [0-9]
\D 匹配非數字: [^\d]
\s 匹配所有空格字符,等同於: [\t\n\f\r\p{Z}]
\S 匹配所有非空格字符: [^\s]
\f 匹配一個換頁符
\n 匹配一個換行符
\r 匹配一個回車符
\t 匹配一個製表符
\v 匹配一個垂直製表符
\p 匹配 CR/LF(等同於 \r\n),用來匹配 DOS 行終止符

4. 零寬度斷言(前後預查)

先行斷言和後發斷言(合稱 lookaround)都屬於非捕獲組(用於匹配模式,但不包括在匹配列表中)。當我們需要一個模式的前面或後面有另一個特定的模式時,就可以使用它們。

例如,我們希望從下面的輸入字符串 $4.44$10.88 中獲得所有以 $ 字符開頭的數字,我們將使用以下的正則表達式 (?<=\$)[0-9\.]*。意思是:獲取所有包含 . 並且前面是 $ 的數字。

零寬度斷言如下:

符號 描述
?= 正先行斷言-存在
?! 負先行斷言-排除
?<= 正後發斷言-存在
?<! 負後發斷言-排除

4.1 ?=... 正先行斷言

?=... 正先行斷言,表示第一部分表達式之後必須跟着 ?=...定義的表達式。

返回結果只包含滿足匹配條件的第一部分表達式。
定義一個正先行斷言要使用 ()。在括號內部使用一個問號和等號: (?=...)

正先行斷言的內容寫在括號中的等號後面。
例如,表達式 (T|t)he(?=\sfat) 匹配 Thethe,在括號中我們又定義了正先行斷言 (?=\sfat) ,即 Thethe 後面緊跟着 (空格)fat

"(T|t)he(?=\sfat)" => The fat cat sat on the mat.
"(T|t)he(?=\sfat)" => The fat cat sat on the mat.

在線練習

4.2 ?!... 負先行斷言

負先行斷言 ?! 用於篩選所有匹配結果,篩選條件為 其後不跟隨着斷言中定義的格式。
正先行斷言 定義和 負先行斷言 一樣,區別就是 = 替換成 ! 也就是 (?!...)

表達式 (T|t)he(?!\sfat) 匹配 Thethe,且其後不跟着 (空格)fat

"(T|t)he(?!\sfat)" => The fat cat sat on the mat.
"(T|t)he(?!\sfat)" => The fat cat sat on the mat.

在線練習

4.3 ?<= ... 正後發斷言

正後發斷言 記作(?<=...) 用於篩選所有匹配結果,篩選條件為 其前跟隨着斷言中定義的格式。
例如,表達式 (?<=(T|t)he\s)(fat|mat) 匹配 fatmat,且其前跟着 Thethe

"(?<=(T|t)he\s)(fat|mat)" => The fat cat sat on the mat.
"(?<=(T|t)he\s)(fat|mat)" => The fat cat sat on the mat.

在線練習

4.4 ?<!... 負後發斷言

負後發斷言 記作 (?<!...) 用於篩選所有匹配結果,篩選條件為 其前不跟隨着斷言中定義的格式。
例如,表達式 (?<!(T|t)he\s)(cat) 匹配 cat,且其前不跟着 Thethe

"(?<!(T|t)he\s)(cat)" => The cat sat on cat.
"(?<!(T|t)he\s)(cat)" => The cat sat on cat.

在線練習

5. 標誌

標誌也叫模式修正符,因為它可以用來修改表達式的搜索結果。
這些標誌可以任意的組合使用,它也是整個正則表達式的一部分。

標誌 描述
i 忽略大小寫。
g 全局搜索。
m 多行修飾符:錨點元字符 ^ $ 工作範圍在每行的起始。

5.1 忽略大小寫 (Case Insensitive)

修飾語 i 用於忽略大小寫。
例如,表達式 /The/gi 表示在全局搜索 The,在後面的 i 將其條件修改為忽略大小寫,則變成搜索 theTheg 表示全局搜索。

"The" => The fat cat sat on the mat.
"The" => The fat cat sat on the mat.

在線練習

"/The/gi" => The fat cat sat on the mat.
"/The/gi" => The fat cat sat on the mat.

在線練習

修飾符 g 常用於執行一個全局搜索匹配,即(不僅僅返回第一個匹配的,而是返回全部)。
例如,表達式 /.(at)/g 表示搜索 任意字符(除了換行)+ at,並返回全部結果。

"/.(at)/" => The fat cat sat on the mat.
"/.(at)/" => The fat cat sat on the mat.

在線練習

"/.(at)/g" => The fat cat sat on the mat.
"/.(at)/g" => The fat cat sat on the mat.

在線練習

5.3 多行修飾符 (Multiline)

多行修飾符 m 常用於執行一個多行匹配。

像之前介紹的 (^,$) 用於檢查格式是否是在待檢測字符串的開頭或結尾。但我們如果想要它在每行的開頭和結尾生效,我們需要用到多行修飾符 m

例如,表達式 /at(.)?$/gm 表示小寫字符 a 後跟小寫字符 t ,末尾可選除換行符外任意字符。根據 m 修飾符,現在表達式匹配每行的結尾。

"/.at(.)?$/" => The fat
                cat sat
                on the mat.
"/.at(.)?$/" => The fat
                cat sat
                on the mat.

在線練習

"/.at(.)?$/gm" => The fat
                  cat sat
                  on the mat.
"/.at(.)?$/gm" => The fat
                  cat sat
                  on the mat.

在線練習

6. 貪婪匹配與惰性匹配 (Greedy vs lazy matching)

正則表達式默認採用貪婪匹配模式,在該模式下意味着會匹配儘可能長的子串。我們可以使用 ? 將貪婪匹配模式轉化為惰性匹配模式。

"/(.*at)/" => The fat cat sat on the mat. 
"/(.*at)/" => The fat cat sat on the mat. 

在線練習

"/(.*?at)/" => The fat cat sat on the mat. 
"/(.*?at)/" => The fat cat sat on the mat. 

在線練習

Tags: