Skip to main content

When working with code in Visual Studio Code, you may need to search for specific instances of a function or method, such as self.logger.log. However, it can be frustrating to sift through lines that are commented out, like # self.logger.log. Fortunately, VS Code provides a powerful search feature that allows you to exclude these commented lines from your search results.

 

The challenge

Suppose you want to search for all occurrences of self.logger.log in your codebase, but you don't want to include lines that have been commented out. For instance, you want to find this:

self.logger.log(f"Invalid chunk structure - missing key choices, current chunk: {chunk}", level='ERROR')

But you want to exclude lines like this:

# self.logger.log(f"Response from OpenAI API initiated", level='INFO')

 

Using a regular expression to search

Visual Studio Code allows you to use regular expressions in the search feature, which can help you achieve this. Here's how you can set up a search that finds self.logger.log while excluding commented lines:

  • Open the search panel: In VS Code, press Ctrl + Shift + F (Windows/Linux) or Cmd + Shift + F (Mac) to open the search panel.
  • Enter the regular expression: In the search box, enter the following regular expression:

    ^[^#]*\bself\.logger\.log\b
  • Enable regular expression search: Make sure to click the "Use Regular Expression" option, which is represented by the .* icon next to the search box.

 

Excluding lines containing specific terms

If you'd like to go a step further and exclude lines containing certain terms, like ERROR, you can modify the regular expression to use a negative lookahead. To exclude lines with ERROR, you would adjust the regex to:

^[^#]*\bself\.logger\.log\b(?!.*\bERROR\b)

This pattern ensures that the search finds self.logger.log but skips any line containing the word ERROR.


 

Understanding the regular expression

Let's break down what this regular expression does:

  • ^ asserts the position at the start of the line. This ensures that the search begins at the start of each line;
  • [^#]* matches any character except the # symbol zero or more times. This part of the expression ensures that if a line starts with # or has # before self.logger.log, it will not be matched;
  • \b asserts a word boundary. This makes sure that self.logger.log is matched as a complete word, rather than part of a longer word or variable name
  • self\.logger\.log matches the exact string self.logger.log. The backslashes escape the periods, ensuring they are treated as literal periods rather than wildcard characters.

 

The wrap

By using this regular expression in Visual Studio Code, you can easily search for self.logger.log in your codebase while excluding lines that are commented out. This can save you time and help you focus on the relevant parts of your code.

Related articles

Andrew Fletcher21 Sep 2024
Counting term occurrences in JSON arrays using regex in VS Code
Working on a project where the JSON dataset contains over 460,000 named records, we are preparing to upsert these records into Pinecone. However, for validation and testing purposes, it's essential to cross-check how many times a specific term appears across the dataset. To ensure data integrity, we...
Andrew Fletcher17 Oct 2023
Managing VS Code extensions via terminal
Visual Studio Code (VS Code) allows you to manage extensions using the VS Code Command Line Interface (CLI) called code. With the code CLI, you can install, list, uninstall, and manage extensions from the command line.  To check you have the code prompt running, runcode --versionResponse you're...