| 字元 | 描述 |
|---|---|
^ | 匹配字串開頭。Multiline=True 時也匹配 \n/\r 後 |
$ | 匹配字串結尾。Multiline=True 時也匹配 \n/\r 前 |
\b | 單詞邊界 (字母數字下劃線與非字母數字下劃線交界) |
\B | 非單詞邊界 |
定位符不能加量詞 (如 ^*、\b+),因為位置是零寬度的,不存在「多個位置」疊加。
' 僅匹配行開頭的 "Chapter 1" ~ "Chapter 99"
Pattern = "^Chapter [1-9][0-9]{0,1}"
Pattern = "^Chapter [1-9][0-9]{0,1}$"
' 匹配單詞開頭的 "Cha"
Pattern = "\bCha" ' 匹配 "Chapter" 的 "Cha"
' 匹配單詞結尾的 "ter"
Pattern = "ter\b" ' 匹配 "Chapter" 的 "ter"
' 匹配非單詞邊界的 "apt" (出現在單詞中間)
Pattern = "\Bapt" ' 匹配 "Chapter" 的 "apt",但不匹配 "aptitude" 的 "apt"
Dim re, tests
Set re = New RegExp
re.Global = True
re.IgnoreCase = True
tests = Array( _
"^Chapter [1-9][0-9]{0,1}", _
"^Chapter [1-9][0-9]{0,1}$", _
"\bCha", _
"ter\b", _
"\Bapt" _
)
For Each pat In tests
re.Pattern = pat
Set matches = re.Execute("Chapter 1\nChapter 12\nChapter apt")
WScript.Echo "Pattern: " & pat
For Each m In matches
WScript.Echo " Match: '" & m.Value & "' at " & m.FirstIndex
Next
Next
^ 在 [] 內代表「非」 (如 [^abc]),此處為定位符用法完全不同\b 位置影響匹配方向:開頭匹配詞首,結尾匹配詞尾Multiline 屬性控制 ^/$ 是否匹配行內換行