块查找器-相似功能

我有一个大字符串(超过255个字符),称为strBlockText。该字符串包括随机文本和块号。块号应采用### Block ####-##(IE:245Block6533-56)格式,但有时有人在文本中输入错误的块号格式-例如,## Block ####- ##或### Block ###-##或## Block ###-## ...等。

**注意,这仅适用于纯文本。

我想编写一个能够声明“错误的块号格式”的函数。当块号是胖手指时。

这是我用作示例的文本:

这是一项测试,我们需要弄清楚为什么它不起作用。 24Block1234-23这是一项测试,我们需要弄清楚为什么 不起作用。 245Block4234-14这是我们需要计算的测试 找出为什么这行不通。这是245Block6533-56的测试 需要弄清楚为什么它不起作用。

这是代码...我认为应该可以,但是不能:

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

为此使用正则表达式会更好吗?...是否有不起作用的原因?

iCMS 回答:块查找器-相似功能

使用具有后期绑定的RegExp对象来考虑此Sub:

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
本文链接:https://www.f2er.com/1890692.html

大家都在问