简体   繁体   中英

Regex to match all words in qoutes

I'm trying to write regex to match all the words passed as parameters of sql procedure call.

input ex:

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

so I need to get 'abc' and 'fds'.

Could you help me to write regular expression to get them between "EXEC(UTE)?" and first keyword? List of keywords I have, so if you help me using only INSERT it is okay, I will replace it.

try this :

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

any single quoted value(s) after the 'exec command ' will be captured.

(note: regexr101 can't remember repeated match group captures, but .NET does )

Description

This regex will do the following:

  • match all the quoted words on the first line after the exec keyword
  • other words on other lines will be ignored
  • allow the source string to be all on one line.

Notes

  • you'll have to be careful using insert as an anchor. Consider this string edge case: exec GetNextSequence 'abc', @Insert, @brokerId out, 'fds'
  • the infinite lookbehind (?<=^exec.*?) assumes that you're using the .net Regex engine as many languages do not support repetition characters in lookbehinds.

The Regex

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

Explanation

正则表达式可视化

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

Examples

Sample Text

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

Sample Capture Groups

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM