簡體   English   中英

正則表達式匹配qoutes中的所有單詞

[英]Regex to match all words in qoutes

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

輸入ex:

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就可以,我會替換它。

試試這個

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

將捕獲'exec 命令 '之后的任何單引號值。

(注意:regexr101不記得重復的匹配組捕獲,但.NET確實如此

描述

這個正則表達式將執行以下操作:

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

筆記

  • 你必須小心使用insert作為錨點。 考慮這個字符串邊緣情況: exec GetNextSequence 'abc', @Insert, @brokerId out, 'fds'
  • 無限的lookbehind (?<=^exec.*?)假設您正在使用.net Regex引擎,因為許多語言不支持lookbehinds中的重復字符。

正則表達式

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

說明

正則表達式可視化

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM