Regular Expressions in Visual Studio Code Search

Having spent years developing in C/SIDE I still get a little giddy using Visual Studio Code’s modern IDE features. You know, finding references to a function, renaming symbols, peeking definitions. Also, being able to search across all of your source code. What a dream.

Even better, you can search for a regular expression – that very powerful, if not slightly bonkers, way of searching for text that your pattern(s).

I was searching through my code for “Post Code” – looking fields and variables that we’ve created. But my locals, parameters and function names won’t have a space between “Post” and “Code”. You can match “Post Code” and “PostCode” in one regular expression search.

These expressions can look a bit baffling to start with, but with a little knowledge and practice you can quickly search for specific things and cut down the results you need to look through:

Post ?Code

Space followed by ? indicates that the space is optional – find matches with and without the space.

Or find table fields that include “Post Code”:

field\(\d+;.*Post Code

Find “field” followed by an opening parenthesis (which must be escaped with a backslash), followed by any number of digits (\d = digit, + is one or more of them), followed by a semi-colon, then any number of character (. = any character except a new line), then “Post Code”

Or find function parameters that include Post Code or PostCode:

proc.*\(.*Post ?Code

Find “proc” (as in the keyword “procedure”) followed by any number of characters, then an opening parenthesis then “Post Code” where the space is optional.

Find a match from a group of terms:

(Sales ?Line|Purch.*Line)

Find “Sales Line” with an optional space and “Purch Line” with any number of characters in the middle (to match PurchLine, PurchaseLine, Purch. Line, Purchase Line etc.)

Regex cheat sheet: https://duckduckgo.com/?q=regex+cheat+sheet&ia=cheatsheet&iax=1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s