regex - 正则表达式不触发 VBA
问题描述
这是我的正则表达式:
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
使用它的代码:
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
此代码使用无效字符触发,但长度大于 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)
谁能这么好心地指出我错过了什么?
控件的视觉效果:
解决方案
您的正则表达式匹配有效输入。因此,您需要.Test(your_string)
并且如果结果是False
,则需要触发错误。
代替
Set vbRegXMatch = vbRegX.Execute(Me.txtProduct.Text)
If vbRegXMatch.Count = 1 Then
和
If vbRegX.Test("1234555") = False Then
此外,由于您期望单场比赛,请使用
.Global = False
推荐阅读
- javascript - 如果之前的答案是正确的,我怎样才能让我的测验机器人问另一个问题?
- ruby-on-rails - POST 请求在 Postman 中有效,但被 React 前端阻止。没有CORS错误,没有进入服务器
- python - Try-finally vs :with pool as"
- java - 如何在 JSch 中设置 Java 主页以执行命令?
- azure - Azure 函数应用部署与 VS 2017 一起使用,但不适用于 Azure Web App 部署任务
- asp.net-mvc - 如何防止我的模型的字符串属性在视图中转换为美国格式日期?
- c# - texbox 的 SSRS 值表达式使用没有范围的聚合表达式。多个数据集,多个表
- excel - VBA(Excel)平均更精确,工作表函数或逐步
- azure - 无法获取 Azure Oauth2 令牌
- c++ - Clang 拒绝编译 libstdc++ 的
标题