Regex in VSCode
Created on January 22, 2023 at 4:50 pm
Learn how Visual Studio Code (VSCode) implements regular expressions (regex) to help you perform your complex pattern-matching duties more effectively. In addition, I will introduce a few useful regex extensions to VSCode.
In this post, I assume you’re already familiar with regular expression (regex) syntax, and you want to know more about how regex is implemented in VSCode. It turns out that this is quite an interesting story, with lots of conflicting information online.
Well, I chatted with some VSCode developers at Microsoft to make sure I understood how it really works under the hood. Allow me to share my distilled findings with you.
Two regular expression engines
As you know, VSCode includes two search experiences. There’s the Search command, accessible from the Activity Bar, that searches all files in the current workspace. Then there’s the document Find command, available from an open editor pane.
To make it clear, let’s use the following terminology to describe VSCode’s two search functions:
- Search: The magnifying glass icon on the Activity sidebar that searches all folders in the current workspace
- Find: Simple find/replace tool available for the current document only (accessible by clicking Edit > Find)
What you might not know is that VSCode uses multiple regex engines under the hood to ensure VSCode works the same, regardless of your operating system.
The workspace search in VSCode is powered by the open-source ripgrep line-oriented search tool. The ripgrep engine uses Rust regex under the hood. What’s cool, though, is that if the ripgrep engine is unable to parse a regex, VSCode falls back to the Perl Compatible Regular Expressions version 2 (PCRE2) engine to process the result.
For example, ripgrep doesn’t support backreferences and lookarounds, so if you use those in search, VSCode will invoke the PCRE2 engine automatically.
You will find an occasional behavior difference between the VSCode Search and Find engines. One of these differences has to do with line endings. Here’s the difference:
- In the editor Find, you can match both CRLF and LF line endings by using the \n metacharacter
- In Search, \n matches only CRLF
Why not the .NET regex engine?
You might wonder, “Why doesn’t Visual Studio Code use the .NET regex engine by default? After all, Microsoft makes .NET, Visual Studio Code, PowerShell, and so forth.”
It’s a good question, and the answer lies in these words: cross-platform compatibility. VSCode should work identically, regardless of whether you use the app on Windows, macOS, or Linux.
In case you’re interested, .NET uses the Nondeterministic Finite Automation (NFA) regex engine. This engine is used by Emacs, Perl, Python, and Tcl, and is much faster than the Deterministic Finite Automation (DFA) engine used by regex-aware Linux tools like awk, egrep, and lex.
File exclusions—A potential point of confusion
By default, VSCode Search doesn’t return search results that reference files ignored by your repository’s .gitignore, .ignore, .rgignore, and /git/info/exclude files.
You can override this default behavior by disabling the files of the exclude filter in VSCode Search, as shown in the following screenshot.
The VSCode software engineers I spoke to told me that a common point of customer confusion concerns the files to exclude control. Specifically, you use glob patterns and not regular expressions to add your filter. In other words, to exclude all YML files from your search results, you would add.yml.
Useful VSCode regex extensions
Now that we understand how VSCode implements regular expressions, let’s turn our attention to the Visual Studio Marketplace and regex-oriented extensions.
You’ll find these extensions offer some regex-oriented convenience to make your development a bit easier. For instance:
- Regex Snippets: Easily insert regex patterns by using a text trigger
- Align by RegEx: Use a regex to apply alignment to multiple selected lines of text
- Regex Workbench: An in-app regex tester
- Regex Previewer: See regex matches in your target document in a separate editor pane
Subscribe to 4sysops newsletter!
I hope you now have a clearer picture of how Visual Studio Code implements regular expressions. If you’re a developer, remember that because VSCode is an open-source project itself, you’re welcome to fork and clone the project to inspect the source code and possibly submit your own bugfixes and new features via pull request.