1

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.

Ilya Livshits
  • 95
  • 1
  • 9
  • 1
    In order for us to 'help' you, we need to see what regex you've tried. – Broots Waymb Apr 29 '16 at 14:08
  • [`(?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+out%2c+%27fds%27%0d%0ainsert+into+%5bttt%5d(id%2c+code%2c+description%2c+startDate%2c+endDate)%0d%0avalues+(%40bid%2c+%40code%2c+%40code%2c+getdate()%2c+%27099999%27)%0d%0a....) – Wiktor Stribiżew Apr 29 '16 at 19:12

2 Answers2

0

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)

Community
  • 1
  • 1
Scott Weaver
  • 7,192
  • 2
  • 31
  • 43
0

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

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

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
Ro Yo Mi
  • 14,790
  • 5
  • 35
  • 43