简体   繁体   English

正则表达式匹配qoutes中的所有单词

[英]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. 我正在尝试编写正则表达式来匹配作为sql过程调用的参数传递的所有单词。

input ex: 输入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'. 所以我需要得到'abc'和'fds'。

Could you help me to write regular expression to get them between "EXEC(UTE)?" 你能帮我写一下正则表达式,让它们介于“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. 我拥有的关键字列表,所以如果你帮我使用INSERT就可以,我会替换它。

try this : 试试这个

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

any single quoted value(s) after the 'exec command ' will be captured. 将捕获'exec 命令 '之后的任何单引号值。

(note: regexr101 can't remember repeated match group captures, but .NET does ) (注意:regexr101不记得重复的匹配组捕获,但.NET确实如此

Description 描述

This regex will do the following: 这个正则表达式将执行以下操作:

  • match all the quoted words on the first line after the exec keyword 匹配exec关键字后第一行上的所有引用单词
  • 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. 你必须小心使用insert作为锚点。 Consider this string edge case: exec GetNextSequence 'abc', @Insert, @brokerId out, 'fds' 考虑这个字符串边缘情况: 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. 无限的lookbehind (?<=^exec.*?)假设您正在使用.net Regex引擎,因为许多语言不支持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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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