Pattern 属性

定义正则表达式模式。

属性用法:

语法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 与模式匹配,它将触发 "Valid format" 节代码流。否则,它将进入与 "Invalid format" 相关的代码流。