2

我有一个需要字符串比较的 vbscript 函数,其中一个/两个字符串可能包含通配符。不幸的是,strcomp("string1","string.*")=0它对我不起作用,因为它正在进行比较并将.*正则表达式通配符视为文字而不是通配符。

如何比较两个字符串,其中一个和/或两个都包含通配符?

功能:

Function webtableCheck(pageName, tableProperty, rowNum, colNum, checkValue)
   Dim x, y, oD, oC
    x = split(tableProperty,":=")
   If checkValue <> "" Then
        Set oD = description.Create
        oD("micclass").value = "WebTable"
        oD(x(0)).value = x(1)
        Set oC = pageName.childobjects(oD)
        y = oC(0).getcelldata(rowNum, colNum)
        msgbox(y)
        If y=checkValue Then
            reporter.ReportEvent micPass, "WebTable Checkpoint", "The webtable checkpoint object """ &  checkValue & """ was found."
        Elseif  **strcomp(y,checkValue,1)** = 0 Then
            reporter.ReportEvent micPass, "WebTable Checkpoint", "The webtable checkpoint object """ &  checkValue & """ was found, however the casing does not match."
        Elseif strcomp(trim(y),trim(checkValue)) = 0 Then
            reporter.ReportEvent micPass, "WebTable Checkpoint", "The webtable checkpoint object """ &  checkValue & """ was found, however leading/lagging spaces not included in datatable and/or webtable cell was found."
        Elseif instr(1,y,checkValue,1) Then
            reporter.ReportEvent micPass, "WebTable Checkpoint", "The webtable checkpoint object """ &  checkValue & """ was found., however a line break or other hidden character was found in the webtable."
        Else
            reporter.ReportEvent micFail, "WebTable Checkpoint", "The webtable checkpoint object """ &  checkValue & """ was not found."
        End If
    End If
End Function
4

1 回答 1

6

您需要使用正则表达式,这里是一个示例。

line = "this is the text to look in to, it contains the searchpattern"
Set RE = New RegExp
RE.IgnoreCase = True
RE.Pattern = "search.*tern"
If RE.Test(Line) Then WScript.echo "found"

.* 是一个正则表达式, . 代表任何字符,* 代表前一个没有或多次出现,您也可以在此处使用 .+,其中 + 表示至少出现一次。您可以在 Internet 上找到大量关于正则表达式的示例和源材料,仅考虑到 Vbscript 使用不那么标准的形式,因此请务必将其包含在您的搜索中。

http://www.mikesdotnetting.com/Article/24/Regular-Expressions-and-VBScript

http://www.regular-expressions.info/vbscriptexample.html

于 2013-05-22T08:26:18.860 回答