2017-07-03 51 views
0

我需要一個模式,做一個非常具體的事情,但幾小時後,我不能達到預期的結果。VBScript正則表達式模式

樣本串:

SELECT col1 FROM tbl1 WHERE col1 = (SELECT col2 FROM tbl2 WHERE col2=col2) 

預期結果:

FROM tbl1 WHERE col1 = (SELECT col2 FROM tbl2 WHERE col2=col2) 

    -> tbl1 
    -> WHERE col1 = (SELECT col2 FROM tbl2 WHERE col2=col2) 

實際圖案:

FROM\s+([^\s,]+)[\s\S]+(WHERE[\s\S]+) 

實際結果:

FROM tbl1 WHERE col1 = (SELECT col2 FROM tbl2 WHERE col2=col2) 

    -> tbl2 
    -> WHERE col2=col2) 

我已經嘗試使用向前看和其他的東西,但我不能讓它從第一個'地方'組。

注意:'tbl1'和'WHERE'之間應該匹配所有可能的內容,而不僅僅是空格。注意2:它應該在第一個'WHERE'後面分組,即使沒有以後的地方。

+0

您應該公佈在典型的輸入/輸出對和列表您的策略中最少提示從匹配對象產生輸出。 –

+1

爲什麼VB6標籤? – DaveInCaz

回答

1

直到有更多的細節公佈,我聲稱,非貪婪模式會解決這個問題:

Option Explicit 

Dim r : Set r = New RegExp 
'r.Pattern = "FROM\s+([^\s,]+)[\s\S]+(WHERE[\s\S]+)" 
r.Pattern = "FROM\s+([^\s,]+)[\s\S]+?(WHERE[\s\S]+)" 
'Dim s : s = "SELECT col1 FROM tbl1 WHERE col1 = (SELECT col2 FROM tbl2 WHERE col2=col2)" 
Dim s : s = "SELECT col1 FROM tbl1 WHERE col1 = (No W h e r e here)" 
Dim m : Set m = r.Execute(s) 
If 1 = m.Count Then 
    Dim t : t = Join(Array("From", m(0).SubMatches(0), m(0).SubMatches(1), _ 
          vbCrLf, vbCrLf, "->", m(0).SubMatches(0), vbCrLf, "->", m(0).SubMatches(1))) 
    WScript.Echo s 
    WScript.Echo t 
Else 
    WScript.Echo "no match" 
End If 

輸出:

cscript 44890052.vbs 
SELECT col1 FROM tbl1 WHERE col1 = (No W h e r e here) 
From tbl1 WHERE col1 = (No W h e r e here) 

-> tbl1 
-> WHERE col1 = (No W h e r e here) 
+0

它很好地工作,即使第二個'在哪裏'。謝謝! – MauriF