块查找器 - 类似功能
Block Finder - Like Function
我有一个名为 strBlockText 的大字符串(超过 255 个字符)。该字符串包括随机文本和块号。区块编号的格式应为###Block####-##(IE:245Block6533-56)但有时有人会在文本中输入错误的区块编号格式 - 例如##Block####- ## 或###Block###-## 或##Block###-##...等
**注意,这仅适用于纯文本。
我想编写一个能够声明“识别出错误的块号格式”的函数。当块号是胖手指时。
这是我用作示例的文本:
This is a Test that we need to figure out why this isn’t working.
24Block1234-23 This is a Test that we need to figure out why this
isn’t working. 245Block4234-14 This is a Test that we need to figure
out why this isn’t working. This is a Test that 245Block6533-56 we
need to figure out why this isn’t working.
这是代码...我觉得应该可以但不是:
Dim strBlockText As String
Dim strBlockCheck As String
If (((strBlockText Like "*##Block####-##*") or _
(strBlockText Like "*###Block###-##*") or _
(strBlockText Like "*##Block###-##*")) And _
(Not strBlockText Like "*###Block####-##*")) Then
strBlockCheck = "Wrong block number format identified."
Else
strBlockCheck = "Block number format acceptable."
End If
为此使用正则表达式而不是 like 会更好吗?...是否有 like 不起作用的原因?
考虑这个 Sub 使用带有后期绑定的 RegExp 对象:
Sub testRegExp2(strS)
Dim regexOne As Object, Matches As Object, Match As Object
'Set regexOne = New RegExp
Set regexOne = CreateObject("VBScript.RegExp")
regexOne.Pattern = "[0-9]+Block[0-9]+-[0-9]+"
regexOne.Global = True
Set Matches = regexOne.Execute(strS)
For Each Match In Matches
If Not Match Like "###Block####-##" Then
Debug.Print "Wrong block number format identified: " & Match
Else
Debug.Print "Block number format acceptable: " & Match
End If
Next
End Sub
我有一个名为 strBlockText 的大字符串(超过 255 个字符)。该字符串包括随机文本和块号。区块编号的格式应为###Block####-##(IE:245Block6533-56)但有时有人会在文本中输入错误的区块编号格式 - 例如##Block####- ## 或###Block###-## 或##Block###-##...等
**注意,这仅适用于纯文本。
我想编写一个能够声明“识别出错误的块号格式”的函数。当块号是胖手指时。
这是我用作示例的文本:
This is a Test that we need to figure out why this isn’t working. 24Block1234-23 This is a Test that we need to figure out why this isn’t working. 245Block4234-14 This is a Test that we need to figure out why this isn’t working. This is a Test that 245Block6533-56 we need to figure out why this isn’t working.
这是代码...我觉得应该可以但不是:
Dim strBlockText As String
Dim strBlockCheck As String
If (((strBlockText Like "*##Block####-##*") or _
(strBlockText Like "*###Block###-##*") or _
(strBlockText Like "*##Block###-##*")) And _
(Not strBlockText Like "*###Block####-##*")) Then
strBlockCheck = "Wrong block number format identified."
Else
strBlockCheck = "Block number format acceptable."
End If
为此使用正则表达式而不是 like 会更好吗?...是否有 like 不起作用的原因?
考虑这个 Sub 使用带有后期绑定的 RegExp 对象:
Sub testRegExp2(strS)
Dim regexOne As Object, Matches As Object, Match As Object
'Set regexOne = New RegExp
Set regexOne = CreateObject("VBScript.RegExp")
regexOne.Pattern = "[0-9]+Block[0-9]+-[0-9]+"
regexOne.Global = True
Set Matches = regexOne.Execute(strS)
For Each Match In Matches
If Not Match Like "###Block####-##" Then
Debug.Print "Wrong block number format identified: " & Match
Else
Debug.Print "Block number format acceptable: " & Match
End If
Next
End Sub