2016-04-29 88 views
1

我正在嘗試編寫正則表達式來匹配作爲sql過程調用參數傳遞的所有單詞。正則表達式匹配qoutes中的所有單詞

輸入例如:

exec GetNextSequence 'abc', @brokerId out, 'fds' 
insert into [ttt](id, code, description, startDate, endDate) 
values (@bid, @code, @code, getdate(), '099999') 
.... 

,所以我需要得到 'ABC' 和 'FDS'。

你能幫我寫一些正則表達式來讓他們介於「EXEC(UTE)?」之間嗎?和第一個關鍵字?我有的關鍵字列表,所以如果你只幫我使用INSERT,沒關係,我會替換它。

+1

爲了讓我們「幫助」你,我們需要看到正則表達式你已經試過。 –

+0

['(?s)(?<= exec(?:ute)?(?:(?!\ b(?:insert | OTHER_KEYWORDS)\ b)。)*?)'([^'] *)' '](http://regexstorm.net/tester?P =(%3FS)(%3F%3C%3dexec(%3F%3aute)%3F(%3F%3A(%3F!%5cbinsert%5CB)。)*%3F)%27(%5B%5E%27 %5D *)%27&I = EXEC + GetNextSequence +%27abc%27%2C +%40brokerId +出來%2C +%27fds%27%0D%0ainsert +入+%5bttt%5D(ID%2C +代碼%2C +描述%2C +的startDate %2C +結束日期)%0D%0avalues +(%40bid%2C +%40code%2C +%40code%2C + GETDATE()%2C +%27099999%27)%0D 0A%....) –

回答

0

說明

此正則表達式將做到以下幾點:第一

  • 匹配所有引用的話行後exec關鍵字
  • 其他行上的其他字將被忽略
  • 允許源字符串全部在一行上。

注意

  • 你必須使用insert作爲錨要小心。考慮這個字符串邊緣情況:exec GetNextSequence 'abc', @Insert, @brokerId out, 'fds'
  • 無限的後視(?<=^exec.*?)假定您使用.net正則表達式引擎,因爲很多語言不支持向後看中的重複字符。

正則表達式

(?<=^exec.*?)'((?:(?!'|\n).)*)' 

說明

Regular expression visualization

NODE      EXPLANATION 
-------------------------------------------------------------------------------- 
    (?<=      look behind to see if there is: 
-------------------------------------------------------------------------------- 
    ^      the beginning of the string 
-------------------------------------------------------------------------------- 
    exec      'exec' 
-------------------------------------------------------------------------------- 
    .*?      any character except \n (0 or more times 
          (matching the least amount possible)) 
-------------------------------------------------------------------------------- 
)      end of look-behind 
-------------------------------------------------------------------------------- 
    '      single quote character 
-------------------------------------------------------------------------------- 
    (      group and capture to \1: 
-------------------------------------------------------------------------------- 
    (?:      group, but do not capture (0 or more 
          times (matching the most amount 
          possible)): 
-------------------------------------------------------------------------------- 
     (?!      look ahead to see if there is not: 
-------------------------------------------------------------------------------- 
     '      single quote character 
-------------------------------------------------------------------------------- 
     |      OR 
-------------------------------------------------------------------------------- 
     \n      '\n' (newline) 
-------------------------------------------------------------------------------- 
    )      end of look-ahead 
-------------------------------------------------------------------------------- 
     .      any character 
-------------------------------------------------------------------------------- 
    )*      end of grouping 
-------------------------------------------------------------------------------- 
)      end of \1 
-------------------------------------------------------------------------------- 
    '      single quote character 

實例

示例文本

exec GetNextSequence 'abc', @brokerId out, 'fds' 
insert into [ttt](id, code, description, startDate, endDate) 
values (@bid, @code, @code, getdate(), '099999') 

獲取樣本組

[0][0] = 'abc' 
[0][1] = abc 

[1][0] = 'fds' 
[1][1] = fds 
0

try this

exec \w+ (?:.*?'(?<quotedWord>\w+)')+ 

的 'EXEC 命令' 之後的任何單引號值(一個或多個)將被捕獲。

(注:regexr101記不清重複匹配組拍攝,但.NET does

相關問題