SSMS regular expressions: format text into a block

Sean’s RegEx for DBAs class must be finally sinking in for me. Today I was able to figure out a RegEx thing in SQL Server Management Studio.

Here’s the problem: Sometimes I document an SP in MS Word, then copy-paste the paragraph into a comment block in the SP. Of course, when I do this, the paragraph is in one big long string:

sp1

It’s a little thing, but I really want to be able to READ the comments. What I want, then, is to go about 40 characters, find a space, and start a newline. But not by hand.

That’s the pseudocode for the RegEx solution, as it turns out.

 

sp3

The breakdown of “Find what”, {.^40} :

  • “.^40” says “find any 40 characters”.
  • The curly braces “tag” the expression found – we can then reference those found characters later.
  • The space after the closing curly brace (which you can’t really see) says “I want to find 40 characters, but they MUST BE FOLLOWED by a space”.  If I didn’t add the space, we’d insert newlines in the middle of words, instead of at the end.

The breakdown of “Replace with”, \1 \n:

  • “\1” references that found expression, the 40 characters discovered in the curly braces above. I want to keep those, so I start my “replace with” with the actual characters found.
  • “\n” is a newline.

The result:

sp4

SO SUPER AWESOME.

Happy days,
Jen McCown
http://www.MidnightDBA.com/Jen

Have you checked out Minion Reindex yet? It’s our free, easy, comprehensive index maintenance solution. You’ll love it, seriously.