VBS使用通配符比较字符串

时间:2013-05-22 01:39:13

标签: regex vbscript

我有一个需要字符串比较的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

1 个答案:

答案 0 :(得分:5)

您需要使用正则表达式,此处为示例。

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"

。*是一个正则表达式,。代表任何字符,*代表前一个或多个出现,你也可以使用。+这里+ mean表示至少有一个出现。 您可以在互联网上找到关于正则表达式的众多示例和源材料,只考虑到Vbscript使用不那么标准的表单,因此请务必在搜索中包含该表单。

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

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

相关问题