Visual Studio Code Regular Expression



Regular Expression Tester

Features

The tool provides the following features:

  • Parsing of your existing regular expressions from code for C# and VB.
  • Parentheses matching.
  • Instant compilation of your regular expressions as you type along with error message feedback.
  • Matches, groups and captures highlighted in color on your input data so you get an easy overview of exactly how your regular expression matches your data.
  • Saving regular expressions for later use.
  • Evaluation is performed in a background thread so it can be aborted if it gets stuck on an inefficient regular expression (try for instance (a|aa)*b on aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)

Quick guide

The tool is available from the right-click menu in the code editor or by shortcut Ctrl R, Ctrl X. You can use it in the following ways:

  1. Open the Regular Expression Tester without selecting any text in the code editor. You will get an empty window, where you can start creating your regular expression with options. When you select 'Insert', a statement like new Regex(@'s*as*', RegexOptions.IgnoreCase | RegexOptions.Compiled) is generated.
  2. Select a string containing a regular expression including quotes - e.g. '(a*b)c' - and start the editor. Your expression will be in the regular expression field to test. When you're done, select 'Insert' and the string in your code window will be updated.
  3. Select a string containing a regular expression and options - e.g. ('(a*b)c' + 'd*[^ef]*' | RegexOptions.Compiled) - and start the editor. Your expression will be in the regular expression field to test and the options will be selected. When you're done select 'Insert'.

You can save your regular expressions for later use, by clicking 'Save' when you're editing a regular expression. All your saved expressions are available from the 'Manage Regular Expressions' tab. Double clicking one of them in this view will open it for edit.


GitHub

Source code is available on GitHub: https://github.com/andreas2411/vsregextesterextension

Online version

There is an online version of this tool available athttp://dotnetregexevaluator.andreasandersen.dk

  1. Visual Studio Code Regular Expression Find
  2. Vscode Regular Expression

Open the Regular Expression Tester without selecting any text in the code editor. You will get an empty window, where you can start creating your regular expression with options. When you select 'Insert', a statement like new Regex(@'s.as.', RegexOptions.IgnoreCase RegexOptions.Compiled) is generated. Select a string containing a regular expression including quotes - e.g. '(a.b)c' - and start the editor. Developer community 2. Search Search Microsoft.com.

Visual Studio Code Regular Expression

When you want to search and replace specific patterns of text, use regular expressions. They can help you in pattern matching, parsing, filtering of results, and so on. Once you learn the regex syntax, you can use it for almost any language.

  1. Press Ctrl+R to open the search and replace pane.

    If you need to search and replace in more than one file, press Ctrl+Shift+R.

  2. Enter a search string in the top field and a replace string in the bottom field.

    Click to enable regular expressions. If you want to check the synax of regular expressions, hover over and click the Show expressions help link.

  3. When you search for a text string that contains special regex symbols, JetBrains Rider automatically escapes them with backlash in the search field.

    Keep in mind that if you copy (Ctrl+C) the string first and then paste (Ctrl+V) it in the search field, the regex symbols will not be taken into account.

    However, when you specifically search for metacharacters such as .[{()^$|?*+, you need to escape them with backslash , so they can be recognized.

    For example, if you need to find ., type . in the search field.

  4. JetBrains Rider can also match a letter case when you enter a range of characters in your search field.

    For example, if you want to search for only uppercase characters, type the following in the search field:

  5. If is unselected in the search field, JetBrains Rider searches for both lower and upper cases.

    Select in the search field to match the case of the specified range.

  6. When you browse the occurrences, JetBrains Rider displays the replacement hints, so you can view the potential results before clicking the Replace button.

See RegEx syntax for more details.

Use regex capturing groups and backreferences

You can put the regular expressions inside brackets in order to group them. Each group has a number starting with 1, so you can refer to (backreference) them in your replace pattern. Note that the group 0 refers to the entire regular expression. However, you can refer to the captured group not only by a number $n, but also by a name ${name}.

Find and replace a captured group

Let's consider the following code:

<new product='ij' category='105'/> <new product='ij' category='105'/> <new product='ij' category='105'/>
  1. Open the search and replace pane Ctrl+R.

  2. In the search field, enter parentheses () that would indicate a capturing group, for example: stitle='(.*)?'s*(/>*).

  3. In the replace field, backreference such groups by numbers starting with 1, for example:

  4. JetBrains Rider highlights the found occurrences based on your search specifications and displays hints with the replace string.

Switch the character case

You can use regular expressions to change the case of characters that matches some criteria.

Expression
  1. Open the search and replace pane Ctrl+R. Make sure that is selected in the search field.

  2. In the search field enter the search pattern.

  3. In the replace field, depending on what you want to achieve, enter one of the following syntax:

    • l changes a character to lowercase until the next character in the string.
      For example, Bar becomes bar.

    • u changes a character to uppercase until the next character in the string.
      For example, bar becomes Bar.

    • L changes characters to lowercase until the end of the literal string E.
      For example, BAR becomes bar.

    • U changes characters to uppercase until the end of the literal string E.
      For example, bar becomes BAR.

Visual Studio Code Regular Expression Find

Refer to the RegEx syntax reference table for more details.

Find and replace a string

Suppose you want to replace an attribute within an element title with an expanded tag <title></title>, which contains some arbitrary string in double-quotes within.

This is how it's done.

  1. With the XML file opened in the editor, press Ctrl+R. The Replace pane appears on top of the editor.

  2. Since you want to replace all the title attributes, regardless of the actual strings contained therein, use regular expressions. Make sure that the checkbox Regex is selected. Thus, everything you type in the Search and Replace fields will be perceived as the regular expressions.

  3. In the Search field, start typing a regular expression that describes all title attributes.

    stitle='(.*)?'s*(/>*)
    Note that though the regular expression stitle='.*?'s*[/>]* matches contents of the title attribute, it is recommended to capture the groups for referencing them in the Replace field.
  4. Then, in the Replace field, type the following regular expression:

    where $1 refers to the first capture group and $2 refers to the second capture group.
  5. Click Replace, or Replace All.

For the regular expressions replacement preview is shown at the tooltip.

Vscode Regular Expression

This is a full functional replace that can refer to capture groups (.*) (/>) in the search field.

Last modified: 08 March 2021