What flavor of Regex does Visual Studio Code use? - regex

Trying to search-replace in Visual Studio Code, I find that its Regex flavor is different from full Visual Studio. Specifically, I try to declare a named group with string (?<p>[\w]+) which works in Visual Studio but not in Visual Studio Code. It'll complain with the error Invalid group.
Apart from solving this specific issue, I'm looking for information about the flavor of Regexes in Visual Studio Code and where to find documentation about it, so I can help myself with any other questions I might stumble upon.
Full Visual Studio uses .NET Regular Expressions as documented here. This link is mentioned as the documentation for VS Code elsewhere on Stackoverflow, but it's not.

Rust Regex in the Find/Replace in Files Sidebar
Rob Lourens of MSFT wrote that the file search uses Rust regex. The Rust language documentation describes the syntax.
JavaScript Regex in the Find/Replace in File Widget
Alexandru Dima of MSFT wrote that the find widget uses JavaScript regex. As Wicktor commented, ECMAScript 5's documentation describes the syntax. So does the MDN JavaScript Regular Expression Guide.
Test the Difference
The find in files sidebar does not support (?=foobar) whereas the find in file widget does support that lookahead syntax.
Regarding Find/Replace with Groups
To find/replace with groups, use parentheses () to group and $1, $2, $3, $n to replace.
Here is an example.
Before:
After:

Shaun's answer is still correct, however to add an update, recently VS Code added the option to opt into using the Perl based PCRE2 engine. You can enable this through your settings config.
This allows you to perform more advanced regex operations like lookaheads and backreferences. But as noted below, the regex still has to be valid JavaScript regex.
VS Code does support regular expression searches, however,
backreferences and lookaround aren't supported by default. But you can
enable these with the setting search.usePCRE2. This configures ripgrep
to use the PCRE2 regex engine. While PCRE2 supports many other
features, we only support regex expressions that are still valid in
JavaScript, because open editors are still searched using the editor's
JavaScript-based search.
And for a bonus if you ended up here trying to do multi line searches, VS Code recently added that feature as well!

I've found newer information (July 22, 2020) about it.
IllusionMH left the following comment in Github:
ripgrep (compatible with PCRE2) is already used for Find in files
functionality (for not open editors) and JS engine used only for open
editors.

Which regex engine does vscode use? It is now a little more nuanced than previously. The best source is this vscode wiki: Github wiki: Notes on Regular Expression Support:
[At top of document]
This document applies to search (CMD+SHIFT+F/CTRL+SHIFT+F) and
quickopen (CMD+P/CTRL+P). By default, VS Code uses the ripgrep tool to
drive search.
...
[At end of document]
Text search uses two different sets of regular expression engines. The
workspace is searched using ripgrep, which will use the Rust regex
engine, and will fallback to PCRE2 if the regex fails to parse in the
Rust regex engine. The Rust regex engine doesn't support some features
like backreferences and look-around, so if you use those features,
PCRE2 will be used. Open files are searched using a JS regex in the
editor itself. Most of the time, you don't need to worry about this,
but you may see an inconsistency in how some complex regexes are
interpreted, and this can be an explanation. Especially when you see a
regex interpreted one way when a file is open, and another way when it
is not. During a Replace operation, each file will be opened in turn,
and the search query will be run as a JS regex.
Another potential issue is how newlines are handled between ripgrep
and the editor. The editor normalizes newlines, so that you can match
both CRLF and LF line endings just with \n. It's actually not possible
to match \r explicitly in the editor because it is normalized away.
When searching in the workspace, VS Code tries to rewrite a regex so
that \n will match CRLF. But \r\n or \s\n will also match CRLF in
closed files, but not in open files.
Two key points: (1) newlines are handled specially and (2) backrefereences and look-arounds are supported despite using the Rust regex engine - if your regex has a look-around or backreference in it PCRE2 will be used instead of the Rust engine.
More on lookarounds
The Find Widget (Ctrl+F) used for finding within the active editor only supports all lookarounds (lookahead and lookbehind) and those lookarounds can be non-fixed-length. So this will work in the Find Widget: (?<!blah.*).
In a search across files (Ctrl+Shift+F) non-fixed-length lookbehinds DO NOT WORK. Lookaheads can be fixed or non-fixed-length. But non-fixed-length positive or negative lookbehinds do not work and you will get an error message below the search input box: Regex parse error: lookbehind assertion is not fixed length which may not appear until you actually try to run the search.

Related

Spyder regex multiline support?

I cannot find the way to set the find/replace function to use multiline regular expressions in spite of this github statement (2015) from the program author:
FYI I've just added support for multiline regular expressions. It's
still exclusively on my local repository but I'll push the
corresponding changeset to the online repository in the next hours.
How the UI looks like:
There is no options menu or button to select multiline, and the find/replace function is not described in the documentation as far as I know. Is it possible to use multiline regex with find/replace, or was multiline support never introduced?

What regular expression engine does XCode's Find Navigator use

The documentation from Apple, Google searches, and StackOverflow is non-existent with regard to what RegEx engine XCode's Find Navigator uses, making it very difficult to write expressions. For example, I tried to do a negative lookbehind, and it doesn't work. Does anyone know where the hidden documentation lies?
Quick Answer: The ICU Engine. (This is the same engine that is used for NSRegularExpression as well).
Longer version:
The main difference between PCRE and ICU, the two leading regex engines, is PCRE's support for recursion within it's expressions. A simple example of this would be:
\w{3}\d{3}(?R)?
which matches aaa111bbb222 fully. However, entering this into an Xcode (7.1.1) regex find field yields this alert:
This leads us to believe that Xcode is using the ICU engine to power it's search and replace features. This make sense since Xcode is likely built either on top of NSRegularExpression or the underlying libicucore.

Visual Studio Find and Replace with Regex

I want to replace C# attributes with VB.NET, which means, [Serializable] should become <Serializable>.
The pattern (\[)(.+)(\]) does find the results but I don't know how to replace the first and the last groups with the appropriate parenthesis.
I read this page, but I didn't understand how to use the curly braces for F&R, I tried to wrap the groups with it but it didn't work.
If you are using the Productivity Power Tools extension from Microsoft that support normal .NET regexes, what you would put in the textbox for the replacement given your regular expression above is:
<$2>
where $2 refers to the second capture group in your regex, i.e. the text between the brackets.
Note that this only works with the Quick Find from Productivity Power Tools though. The normal find/replace in Visual Studio use another syntax altogether.
Find what: \[{Serializable}\]
Replace with: <\1>

Removing everything between a tag (including the tag itself) using Regex / Eclipse

I'm fairly new to figuring out how Regex works, but this one is just frustrating.
I have a massive XML document with a lot of <description>blahblahblah</description> tags. I want to basically remove any and all instances of <description></description>.
I'm using Eclipse and have tried a few examples of Regex I've found online, but nothing works.
<description>(.*?)</description>
Shouldn't that work?
EDIT:
Here is the actual code.
<description><![CDATA[<center><table><tr><th colspan='2' align='center'><em>Attributes</em></th></tr><tr bgcolor="#E3E3F3"><th>ID</th><td>308</td></tr></table></center>]]></description>
I'm not familiar with Eclipse, but I would expect its regex search facility to use Java's built-in regex flavor. You probably just need to check a box labeled "DOTALL" or "single-line" or something similar, or you can add the corresponding inline modifier to the regex:
(?s)<description>(.*?)</description>
That will allow the . to match newlines, which it doesn't by default.
EDIT: This is assuming there are newlines within the <description> element, which is the only reason I can think of why your regex wouldn't work. I'm also assuming you really are doing a regex search; is that automatic in Eclipse, or do you have to choose between regex and literal searching?

Find and replace in files - .NET friendly regular expression syntax

I'm looking for a decent tool that can do search and replace over multiple files, with regular expression syntax I'm use to in C#. Normally I would do this in Visual Studio, except it has the strangest regex syntax (and this is meant to be faster than just replacing the text in the files manually).
So far I've tried windows grep but it didn't like the regex below. The regex in question is
<see cref="(?<class>.+)">(.+)</see>
To replace with
<see cref="${class}"/>
Alternatively converting this to Visual Studio's syntax would be fine!
Jeff has a whole post on this on his blog.
In Visual Studio, find:
\<see cref="{:i}"\>.*\</see\>
and replace with:
<see cref="\1"/>
{} is the VS grouping operator. It is not named. In replacements \n is used for the value of the n-th group in the find expression.
You can tweak the :i and .* parts if you need to account for new lines or other whitespace.
In Visual Studio you can do this with the following Reg Ex:
\<see cref="{.+}"\>.+\</see\>
Replace:
<see cref="\1" />
Remember to select Use/Regular Expressions in the Find and Replace dialog.
I've long been thinking it sucks that Visual Studio does not support the same Regular Expression syntax as the .Net framework.
There are a few choices:
If it's something you do regularly, I would go with PowerGrep as it is probably the most complete and user-friendly tool for this.
Its sibling, RegexBuddy also has a Grep functionality, albeit not as comprehensive.
If you're not afraid of Perl, you can also use its command line as a search and replace tool.
Otherwise, there is still the venerable sed that works on Windows.
notepad++ is quite popular and lightweight and has this functionality, have you tried that?:
http://notepad-plus.sourceforge.net/uk/site.htm
EDIT:
It would appear my link is outdated and this usage is only quite recent:
http://www.opensourcereleasefeed.com/release/show/notepad-v5-2-released-replace-in-files-feature-added
You can use the .NET Regular Expression AddIn to allow you to use .NET regexes with Visual Studio. It's on CodeProject at http://www.codeproject.com/KB/macros/VS2005RegexAddIn.aspx.
Mike