If you are going to work efficiently as a programmer, then your search skills have got to be on point. You need to be able to Ctrl+F and Ctrl+H yourself out of trouble, and quick! When find-and-replace work gets tricky, that’s when you pull out Regular Expressions, the power tool of searching. You can use some keyboard shortcuts to make faster work, as well. And you can use it in SQL Server Management Studio, Notepad++, Visual Studio Code and many other places.

First thing to know is that there are some characters that are hard to search for, but they are searchable! 

Escape Sequences

Escape sequences are normally two characters long and start with a backslash. They are not the literal characters that are shown, they are a code. These escape sequences can also be used in Find screens that allow regular expressions (regex).

The three that are most important to what we are about to look at are:

\n  – linefeed

\r  – carriage return

\t  – tab

You may have heard of a Carriage Return/Line Feed, or CR-LF. This is the standard line ending in a Microsoft file. A CR-LF would be \r\n

Linux and Unix just use a linefeed – \n

Next thing to know are a couple of keyboard tricks for selecting text.

Keyboard Shortcuts

Anytime you can learn a keyboard shortcut, do it! It will save you time by letting your hands stay on the keyboard instead of flipping over to the mouse.

Shift  – Any time you hold down shift, you can use it to select. 

Ctrl + Left Arrow/Right Arrow – Move cursor to the next word.

Shift + Ctrl + Left Arrow/Right Arrow – Select the word

Shift + End  – Select from the cursor location to the end

Column Select

Column selection is when you select by column instead of by row.

Here is now to make a column selection in a few common apps:

Alt + Shift – SQL Server Management Studio, Notepad++, most others

Ctrl + Shift + Alt – Visual Studio Code

Instead of this:

You select like this:

Being able to select by column will most likely change your life. 

Putting it All Together

So, here’s our example text in SQL Server Management Studio:

First, I’m going to put each section on a line, breaking where the comma is.

  1. Ctrl+H – this will bring up the Find and Replace function
  2. Hit the .* icon to turn it on. .* signifies Regular Expressions
  3. Look at the text. We have a comma, then a space. We want to replace the comma-space with a comma-cr-lf. 
    • Top textbox in the Find-Replace, we enter comma-space
    • Bottom textbox in the Find-Replace, we enter comma-\r\n
    • Replace All! Our text is now broken into lines.

 

 

 

 

 

Next, we want each line to begin with a single quote.

I mean, sure, we could go down and put one on each line, but where’s the fun in that?!

  1. Bring your cursor up to in front of the first line, first character.
  2. Hold down Shift + Alt
  3. Use the Down Arrow to bring the cursor down to the beginning of the last line
  4. Hit the Single Quote key
  5. BAM!

Next, we want to put a single quote and a comma at the end of the lines. Here is where we use the Escape Sequences.

  1. Ctrl-H again and turn on the Regular Expressions again (.*)
  2. Enter the string we are looking for: \r\n
  3. Enter the string we want it to be replaced with:  ‘,\r\n
  4. Hit Alt+A to replace All!
  5. Pow!

That leaves us the last line.

  1. Ctrl+End to the very last character and add your single quote.
  2. Fin!

All together now!

References

Regular expression (Wikipedia)

Windows Support – Keyboard Shortcuts for Windows 10