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.
This regex will do the following:
exec
keyword Notes
insert
as an anchor. Consider this string edge case: exec GetNextSequence 'abc', @Insert, @brokerId out, 'fds'
(?<=^exec.*?)
assumes that you're using the .net Regex engine as many languages do not support repetition characters in lookbehinds. The Regex
(?<=^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
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.