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)

Zombo
  • 1
  • 62
  • 391
  • 407
JonasVH
  • 1
  • 1

1 Answers1

0

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#.

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.

Zombo
  • 1
  • 62
  • 391
  • 407
Heinrich Filter
  • 5,760
  • 1
  • 33
  • 34
  • Not sure about `\w+`, as it doesn't allow spaces. But then again, the OP wrote it, so maybe it should be a single word. – Kobi Nov 26 '09 at 13:33
  • Did you put the first block on one line or something, because I only get PRINT 'text ''test2'' as a match. In other words, the regex doesn't get passed line-breaks. FYI i am using the regex component in C#: new Regex(@"PRINT.*?\'{2}\w+?\'{2}", RegexOptions.Multiline) – JonasVH Nov 26 '09 at 13:36
  • Sorry, I just copied the text form the browser not realizing there were no line-breaks in the first sample. I have will edited my answer accordingly – Heinrich Filter Nov 27 '09 at 08:57
  • Yep, I also came to the conclusion that it probably won't work in one RegEx so I've used your code and guess what ? It works perfectly. –  Jan 23 '11 at 17:14