[英]Regex not Triggering VBA
This is my regex:这是我的正则表达式:
Dim vbRegX As Object, vbRegXMatch As Object
Set vbRegX = CreateObject("vbscript.regexp")
With vbRegX
.Global = True
.IgnoreCase = True
.Pattern = "^[a-zA-Z0-9_-]{1,20}$"
End With
code that uses it:使用它的代码:
Set vbRegXMatch = vbRegX.Execute(Me.txtProduct.Text)
If vbRegXMatch.Count = 1 Then
MsgBox "This string has invalid characters in it. Illegal characters are out side of the following ranges:" & vbNewLine & vbNewling & "a-z or A-Z" & vbNewLine & vbNewling & "0-9, - or _. Please try again."
Cancel = True
Me.txtProduct.SetFocus
Set vbRegXMatch = Nothing
Set vbRegX = Nothing
Exit Sub
End If
This code fires with invalid characters but not when length is > 20. This is the output given to me by Regex Buddy:此代码会使用无效字符触发,但不会在长度大于 20 时触发。这是 Regex Buddy 给我的输出:
Dim FoundMatch As Boolean
Dim myRegExp As RegExp
Set myRegExp = New RegExp
myRegExp.Pattern = "^[a-zA-Z0-9_-]{1,20}$"
FoundMatch = myRegExp.Test(SubjectString)
Can anyone so kindly point out what Im missing?任何人都可以这么友好地指出我缺少什么吗?
visual of the control:控件的可视化:
Your regex matches valid input.您的正则表达式匹配有效输入。 Thus, you need to
.Test(your_string)
and if the result is False
, you need to fire an error.因此,你需要
.Test(your_string)
并且如果结果是False
,你需要触发一个错误。
Replace代替
Set vbRegXMatch = vbRegX.Execute(Me.txtProduct.Text)
If vbRegXMatch.Count = 1 Then
with和
If vbRegX.Test("1234555") = False Then
Also, since you expect a single match, use此外,由于您希望进行单个匹配,请使用
.Global = False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.