Skip to main content

If you're working with a large codebase in Visual Studio Code (VS Code) and need to find specific occurrences of a term, but only when it's a standalone word possibly surrounded by spaces or parentheses, regular expressions (regex) are your best friend. This guide will walk you through the steps to efficiently search for these instances, I'll be searching for the term 'SBT' and ensuring you don't pick up unwanted matches like `'ADSBTCR'` or `'SBT123'`.

 

Why use regular expressions?

Regular expressions allow you to perform complex search patterns in text. By using regex in VS Code, you can pinpoint exact matches that meet specific criteria, saving you time and reducing errors.

Steps to search for 'SBT' with specific conditions

1. Open the search panel

Windows/Linux: Press `Ctrl + Shift + F`;

Mac: Press `Cmd + Shift + F`.

This will open the search panel on the left side of VS Code.

 

2. Enable regular expression search

Click on the `.*` icon in the search bar to enable regex search. This tells VS Code that you're entering a regular expression pattern.

![Enable regex search](https://code.visualstudio.com/assets/docs/editor/codebasics/search-regex.png)

 

3. Enter the regular expression pattern

In the search bar, paste the following regex pattern:

(?<!\w)[\s()]*SBT[\s()]*(?!\w)

 

4. Review the search results

VS Code will highlight all matches in your codebase that fit the pattern. You can click on each result to navigate to its location in your code.

 

5. Optional: Replace matches

If you need to replace the found instances with something else:

  • Enter the replacement text in the "Replace" field.
  • Click on the replace icons to replace matches individually or all at once.

 

Understanding the regex pattern

Let's break down the regular expression to understand how it works:

(?<!\w): Negative lookbehind. Ensures that there is no word character (letters, digits, or underscore) immediately before 'SBT'. This prevents matches like 'ADSBTCR'.
[\s()]*: Matches zero or more whitespace characters (`\s`) or parentheses (` or `). This accounts for any spaces or parentheses around 'SBT'.
SBT: The exact sequence we're searching for.
[\s()]*: Again, matches any spaces or parentheses that might come after 'SBT'.
(?!\w): Negative lookahead. Ensures that there is no word character immediately after 'SBT'.

 

Examples of matches

  • (SBT)
  • ` SBT `
  • `( SBT )`
  • `SBT`
  • `((  SBT  ))`

 

Examples of non-matches

  • `ADSBTCR`
  • `SBT123`
  • `mySBTvalue`

 

Tips and considerations

Ensure regex mode is enabled: Forgetting to enable regex mode is a common mistake.

Check VS Code version: Make sure your VS Code is up-to-date to support advanced regex features like lookbehinds.

Test your regex: If you're unsure, you can test your regex pattern on regex101.com by inputting sample text.

 

The wrap

By leveraging regular expressions in VS Code, you can perform precise searches that significantly enhance your coding efficiency. This method ensures you only find the standalone instances of 'SBT' you're interested in, without picking up unrelated matches embedded within other words.

Related articles

Andrew Fletcher06 Oct 2024
How to search for "text" in VS Code while excluding comments
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...
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. &nbsp;To check you have the code prompt running, runcode --versionResponse you're...