简体   繁体   English

正则表达式:匹配带有引号的SQL PRINT块

[英]Regex: match SQL PRINT blocks with quoted text in it

I have the following text I am trying match using regular expressions: 我尝试使用正则表达式匹配以下文本:

PRINT CONVERT(NVARCHAR, CURRENT_TIMESTAMP, 111) + ' ' + CONVERT(NVARCHAR, CURRENT_TIMESTAMP, 108) + ' -Test Mode : ' + (CASE WHEN @turbo_mode_ind = 1 THEN 'some text ''test'' some more text.' ELSE 'and even more text ''temp'' when will it stop ?' END) PRINT CONVERT(NVARCHAR,CURRENT_TIMESTAMP,111)+''+ CONVERT(NVARCHAR,CURRENT_TIMESTAMP,108)+'-Test Mode:'+(在@turbo_mode_ind = 1的情况下,然后'一些文本``测试''更多文本。' ELSE'甚至更多的文本“ temp”何时会停止?'END)

PRINT 'text don''t text' PRINT'文本不发短信'

PRINT 'text ''test2'' text' PRINT'文本'test2'文本'

What I want to match is: 我要匹配的是:

PRINT CONVERT(NVARCHAR, CURRENT_TIMESTAMP, 111) + ' ' + CONVERT(NVARCHAR, CURRENT_TIMESTAMP, 108) + ' -Test Mode : ' + (CASE WHEN @turbo_mode_ind = 1 THEN 'some text ''test'' PRINT CONVERT(NVARCHAR,CURRENT_TIMESTAMP,111)+''+ CONVERT(NVARCHAR,CURRENT_TIMESTAMP,108)+'-Test Mode:'+(在@turbo_mode_ind = 1的情况下,然后'一些文本``test''

PRINT 'text ''test2'' 打印'text``test2''

So basically I want to match: 所以基本上我想匹配:

  • starting at PRINT 从PRINT开始
  • each char that comes after PRINT (.*) PRINT(。*)之后的每个字符
  • inclusive line-breaks (don't stop at line-breaks) 包含换行符(不要在换行符处停止)
  • with \\'{2}\\w+\\'{2} at the end of the match 在比赛结束时使用\\'{2} \\ w + \\'{2}
  • non-greedy (.*?) 非贪婪(。*?)
  • AND no empty line(s) between PRINT and \\'{2}\\w+\\'{2} 并且在PRINT和\\'{2} \\ w + \\'{2}之间没有空行

I have already compsed this, but it still matches empty line(s): 我已经对此进行了压缩,但是它仍然匹配空行:

PRINT.*?\\'{2}\\w+\\'{2}(?!\\n\\s*\\n)

Edit after comment: 评论后编辑:

Looking at the requirements again I could not come up with a single regex solution quickly. 再次查看需求,我无法快速提出单个正则表达式解决方案。 In your comments you mention that you are using C#. 在您的评论中,您提到您正在使用C#。

A possible solution would therfore be to first split the string at blank lines and then extracting the text. 因此,可能的解决方案是先将字符串拆分为空白行,然后提取文本。

Something like this: 像这样:

string pattern = @"^$";

foreach (string result in Regex.Split(input, pattern, RegexOptions.Multiline) 
{
    Regex rxFindSql = Regex(@"PRINT.*?\'{2}\w+?\'{2}", RegexOptions.SingleLine)       
    MatchCollection matches = rxFindSql.Matches(result);
}  

This should do the trick but I did not test the code. 这应该可以解决问题,但是我没有测试代码。

I hope this helps. 我希望这有帮助。

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

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