2017-10-11 8 views
0

VBScriptが動作していますが、RTMを使用してSQL Serverの複数のバージョンを検索する環境などで、regexを使用して効率を向上させたい:文字列がない場合、「^ 10 *」それはSQL Server 2008を書くべきであるならば、私は、スクリプトを希望 Regex with VBScriptを使用して結果を検索して書き込みする

 
10.5.1720.0 SQL Server 2008 R2 CU2 
10.5.1702.0 SQL Server 2008 R2 CU1 
9.00.4305 SQL Server 2005 SP3 CU10 
9.00.4294 SQL Server 2005 SP3 CU9 
8.00.2039 SQL Server 2000 SP4 
8.00.760 SQL Server 2000 SP3 

を認識するために「^ 9 *」SQL Server 2005を書くというように

これは私のスクリプトです:

Dim mts, objShell, PV 
Set objShell = CreateObject("WScript.Shell") 
Set ArgObj = WScript.Arguments 
PV = "$(PV)" 
Set regEx = New RegExp 
regEx.IgnoreCase = True 
regEx.Global = True 
regEx.MultiLine = True 
regEx.Pattern = "^10\.*" 

If PV = "7.0.623" Then 
    WScript.Echo "7.0" 
ElseIf PV = "7.0.699" Then 
    WScript.Echo "7.0" 
ElseIf PV = "7.0.842" Then 
    WScript.Echo "7.0" 
ElseIf PV = "7.0.961" Then 
    WScript.Echo "7.0" 
ElseIf PV = "7.0.1063" Then 
    WScript.Echo "7.0" 
ElseIf PV = "8.0.194" Then 
    WScript.Echo "2000" 
ElseIf PV = "8.0.384" Then 
    WScript.Echo "2000" 
ElseIf PV = "8.0.532" Then 
    WScript.Echo "2000" 
ElseIf PV = "8.0.760" Then 
    WScript.Echo "2000" 
ElseIf PV = "8.0.2039" Then 
    WScript.Echo "2000" 
ElseIf PV = "13.1.4001.0" Then 
    WScript.Echo "2016" 
Else 
    WScript.Echo "DTP" 
End If 

VBでRegexを使って減らす方法はありますか?

答えて

1

正規表現をSelect Caseと組み合わせることをおすすめします。

Set re = New RegExp 
re.Pattern = "^(\d+)\." 

For Each m In re.Execute(PV) 
    Select Case m.Submatches(0) 
     Case "7" : version = "7.0" 
     Case "8" : version = "2000" 
     Case "9" : version = "2005" 
     Case "10" : version = "2008" 
     Case "13" : version = "2016" 
     Case Else : version = "DTP" 
    End Select 
Next 

WScript.Echo version 
0

異なるアプローチ:代わりにCase(コード)の主要な数と辞書(データ)を取得するためにSplitを使用した:

Option Explicit 

Function makeDict(sK, sV) 
    Dim d : Set d = CreateObject("Scripting.Dictionary") 
    Dim aK : aK = Split(sK) 
    Dim aV : aV = Split(sV) 
    Dim i 
    For i = 0 To UBound(aK) 
     d(aK(i)) = aV(i) 
    Next 
    d(Null) = aV(i) 
    Set makeDict = d 
End Function 


Dim d : Set d = makeDict("10 7", "2008 7.0 DTP") 

Dim ts : Set ts = CreateObject("Scripting.FileSystemObject").OpenTextFile("data.txt") 
Do Until ts.AtEndOfStream 
    Dim s : s = ts.ReadLine() 
    Dim k : k = Split(s, ".")(0) 
    Dim r 
    If d.Exists(k) Then 
     r = d(k) 
    Else 
     r = d(Null) 
    End If 
    WScript.Echo s, "=>", r 
Loop 
ts.Close 

出力:

cscript 46691612-2.vbs 
10.5.1720.0 SQL Server 2008 R2 CU2 => 2008 
10.5.1702.0 SQL Server 2008 R2 CU1 => 2008 
9.00.4305 SQL Server 2005 SP3 CU10 => DTP 
9.00.4294 SQL Server 2005 SP3 CU9 => DTP 
8.00.2039 SQL Server 2000 SP4 => DTP 
8.00.760 SQL Server 2000 SP3 => DTP 
7.00.760 SQL Server sieben => 7.0 
0

私の友人は私を助けましたこのスクリプトはうまくいきました:

Dim mts, objShell, Version 
set objShell = CreateObject("Wscript.Shell") 
Set ArgObj = WScript.Arguments 
Version ="$(Version)" 
erro="DTP" 
a=Split(Version,".") 
IF uBound(a) > 0 Then 
    VersionInt = CInt(a(0)) 
    saida="" 
    IF VersionInt = 7 or VersionInt = 6 Then 
     saida = VersionInt 
    End If 
    IF VersionInt = 8 Then 
     saida = "2000" 
    End If 
    IF VersionInt = 9 Then 
     saida = "2005" 
    End If 
    IF VersionInt = 10 Then 
     saida = "2008" 
     IF uBound(a) > 1 Then 
      ReleaseVersionInt = CInt(a(1)) 
      If ReleaseVersionInt > 49 then 
       saida = "2008 R2" 
      End If 
     End If 
    End If 
    IF VersionInt = 11 Then 
     saida = "2012" 
    End If 
    IF VersionInt = 12 Then 
     saida = "2014" 
    End If 
    IF VersionInt = 13 Then 
     saida = "2016" 
    End If 
    IF VersionInt = 14 Then 
     saida = "2017" 
    End If 
    If saida="" Then 
     Wscript.echo erro 
    else 
     Wscript.echo saida 
    End IF 
Else 
    Wscript.echo erro 
End IF 
関連する問題