正则表达式匹配qoutes

时间:2016-04-29 14:05:51

标签: c# .net regex

我正在尝试编写正则表达式以匹配作为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就可以了,我会替换它。

2 个答案:

答案 0 :(得分:0)

try this

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

&#39; exec 命令之后的任何单引号值&#39;将被捕获​​。

(注意:regexr101不能记住重复的匹配组捕获,但是.NET does

答案 1 :(得分:0)

描述

此正则表达式将执行以下操作:

  • 匹配exec关键字
  • 后第一行的所有引用字词
  • 其他行上的其他字词将被忽略
  • 允许源字符串全部在一行上。

备注

  • 您必须小心使用insert作为锚点。考虑这个字符串边缘情况:exec GetNextSequence 'abc', @Insert, @brokerId out, 'fds'
  • 无限外观(?<=^exec.*?)假设您正在使用.net Regex引擎,因为许多语言不支持外观中的重复字符。

正则表达式

(?<=^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