regex - 经典 ASP 中的多个 RegEx 模式
问题描述
我在经典 ASP 中使用正则表达式进行验证。我想验证两个不同的值,并检查是否只有两个模式中提到的列出的字符和符号用于该值,并且我尝试为此使用两个不同的 Regex 对象。但这对我不起作用
这是我的代码
Set re = New RegExp
With re
.Pattern = "[a-zA-Z0-9\&_+()/-]"
.Global = True
.IgnoreCase = True
End With
Set reNew = New RegExp
With reNew
.Pattern = "[a-zA-Z0-9.!\"£$%^&()_+-=[]{}#:@./<>?\\|]"
.Global = True
.IgnoreCase = True
End With
if re.Test(strComments) = false then
response.write " <label>Upload failed !! Please enter comments using valid characters a-z A-Z 0-9 \._+()%/&-</label>"
response.end
else
if reNew.Test(strremark) = false then
response.write "<label> Upload failed !! Please enter remark using valid characters a-z A-Z 0-9 \._+()%/&-</label>"
response.end
end if
end if
谁能帮我理解我在哪里犯了错误?
解决方案
不需要多个RegExp
对象实例,唯一改变的是模式,它只是一个字符串,所以将字符串存储在一个变量中并替换每个Test()
.
Dim stringToTest: stringToTest = "..." 'Your test string
Dim validationPattern1: validationPattern1 = "..."
Dim validationPattern2: validationPattern2 = "..."
Dim re: Set re = New RegExp
'Set regex properties
With re
.Gloabl = True
.IgnoreCase = True
End With
'Before testing add the pattern
re.Pattern = validationPattern1
If Not re.Test(stringtoTest) Then
'Validation1 failure logic here
'...
End If
'Add the next pattern
re.Pattern = validationPattern2
If Not re.Test(stringtoTest) Then
'Validation2 failure logic here
'...
End If
推荐阅读
- azure - 更改 accountEnabled 会引发 403 Authorization_RequestDenied
- android - 使用动态屏幕时如何防止内存泄漏?
- bash - 在终端中运行 matlab 命令
- grep - 为什么 xargs 的退出代码会因“-I”选项的存在而不同?
- reactjs - 具有解构变量的 React SetState 提供了错误的结果
- angular - 在自定义 Angular 库中添加外部库(Bootstrap、jquery ..)的脚本
- pipenv - 如何使用 HTTPS 和 SSH URL 指定 VCS 依赖项
- python - InvalidArgumentError:您必须使用 dtype float 和 shape 为占位符张量“Placeholder”提供一个值
- c++ - 如何在 r-tree 的内部节点存储一些信息?
- sql - Bill(Master) 和 BillDetail(Detail) 表:使用表连接生成查询