定義正規表示式樣式。
特性用法:
語法:Object.Pattern = "<PATTERN STRING>"
範例:
regExpObj.Pattern = "^\d{4}_\d{6}$" ' 符合 1234_567890 格式的字串
regExpObj.Pattern = "\b\d{2}/\d{2}/\d{4}\b" ' 符合 MM/DD/YYYY 格式的日期
Dim regex, inputText
Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = "^\d{4}_\d{6}$" ' Match strings in the format 1234_567890
inputText = "1234_567890"
' Check if the inputText matches the pattern
If regex.Test(inputText) Then
'Valid Format Related Coding
Else
'Invalid format Related Coding
End If
在上述程式碼中,樣式 ^\d{4}_\d{6}$ 確保 inputText 必須完全符合開頭為 4 位數,後面接著底線 (_),結尾為 6 位數的格式。如果 inputText 符合樣式,就會觸發「有效格式」區段程式碼流程。否則,它會落入「無效格式」相關的程式碼流程。