0

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 'text don''t text'

PRINT 'text ''test2'' text'

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 'text ''test2''

So basically I want to match:

  • starting at PRINT
  • each char that comes after PRINT (.*)
  • inclusive line-breaks (don't stop at line-breaks)
  • with \'{2}\w+\'{2} at the end of the match
  • non-greedy (.*?)
  • AND no empty line(s) between PRINT and \'{2}\w+\'{2}

I have already compsed this, but it still matches empty line(s):

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

4

1 回答 1

0

评论后编辑:

再次查看需求,我无法快速提出单一的正则表达式解决方案。在您的评论中,您提到您正在使用 C#。

因此,一种可能的解决方案是首先在空行处拆分字符串,然后提取文本。

像这样的东西:

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);
}  

这应该可以解决问题,但我没有测试代码。

我希望这有帮助。

于 2009-11-26T12:19:16.947 回答