Regex search causes VS-Code to crash on macbook - regex

I'm trying to search for a RegEx expression using VS-Code's built-in search option.
Here is the regex: (?:((\s|\w+|\d+|\]|\))+)(?<!(\bReact\b))(?<dot>\.(?!\.))+)
As soon as I enter this expression, VS-Code crashes on my macbook.
Is there any way to prevent this?

You have (?<dot>\.(?!\.))+) a named capturing group in your regex. I don't believe that is supported in vscode search across files.
In any case, your original regex froze vscode on my Windows machine, but when I removed the named capturing group, the regex worked fine in BOTH the find in a file widget and searching across files. So this did not freeze vscode for me:
(?:((\s|\w+|\d+|\]|\))+)(?<!(\bReact\b))(\.(?!\.))+)
I suggest you replace the named capturing group with just a simple capturing group.

Related

Using Regular Expressions in Netbeans 8.2 "find & replace"

I am using NetBeans 8.2. I am working in the code editor window and trying to use regex in combination with the NetBeans find/replace feature. I have the regex button turned on.
I am trying this
on this code
specStripWidthUpper: $("#uniflytebundle_quoteitem_QuoteRing_specStripWidthUpper"),
specStripWidthLower: $("#uniflytebundle_quoteitem_QuoteRing_specStripWidthLower"),
The result I would like would take 1st Category found in find regex
specStripWidthUpper
and repeat it on other side of colon ":" like
specStripWidthUpper:specStripWidthUpper
instead it replaces the selection with $1. looking like
specStripWidthUpper:$1,
specStripWidthLower:$1,
Is there a NetBeans setting to run regex for the replace input window or am I doing something incorrect?
Thank you in advance for your time and effort.
Netbeans (8.2?) does not like the lookarounds. I do not know if this is a new thing but you can get around it with a simplified pattern.
However, your pattern does not capture the part you want to repeat, i.e. specStripWidthUpper (you can see this when you toggle the Select option).
Try it like this:
(\w+)(?:\:)(.*),
$1:$1
You might be required to anchor the query to avoid false positives.

Regex for form.controls['property']

I am trying to find replace thousands of strings in code - form.controls['property'] with form.get('property') where property is a variable.
So far I got find expression .controls\['\w+'\] with replace expression .get\('\w+'\) but VSCode is replacing .controls['products'] with .get\('\w+'\) for string productForm.controls['products'].controls.length. I need to replace it with productForm.get('products').controls.length Any ideas how to fix it? Thanks!
Parentheses () capture a group. Money sign $ accesses a group by index.
.controls\['(\w+)'\]
.get('$1')
This is the result in VS Code:
See also: What flavor of Regex does Visual Studio Code use?

What flavor of Regex does Visual Studio Code use?

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.

How to search for a word part without capuring the full word

A bit of context
I'm renaming all instances of a "config" variable to "configuration".
In order to validate that I renamed every existing instance of the variable, I use the Find In Files feature of Visual Studio. I search for "config" and it returns me results for variables named either "config" and "configuration", since a search for config matches both. However, I'm only interested in seeing the "config" ones (which need renaming), not the "configuration" ones (which are correctly named).
It is actually a problem since it's a pain to scroll through hundreds of irrelevant search results.
Here comes the regex
Therefore, I'm looking to use a regex in the search to solve my problem. A regex that would match anything containing config, except if it contains configuration. For example, someConfigVariable should be a match and someConfigurationVariable should not be.
You can use
(Config)(?!uration)
Working Demo
It captures the word Config which can be accessed using $1 for substitution.
Uses Group Capture
If you can use lookaheads: config(?!uration).
\bconfiguration\b|\bconfig\b
Try this.Replace by configuration.See demo.Just capture configuration as well.
https://regex101.com/r/sJ9gM7/19

Specific word followed by full stop followed by another group RegEx

Hi I'm having a little trouble with a regular expression.
I tested:
([Version]+)\.([.0-9A-Za-z]+)
With:
/Downloads/Documents/Access MDB - DEV Version.2.1.4.zip
This worked in RegexHero, my groups seemed fine (sort of).
However when I'm searching through HTML source code it returns things like:
e.axd
How would I get 2 groups:
Version.
2.1.4.zip
Or even one group?
Version.2.1.4.zip
I'm puzzling over this, regular expressions aren't my strong suit.
Version.2.1.4.zip in one group,
^.* (.*)$
DEMO
After using Avinash's answer as a baseline I found the solution as:
(version.*\.zip)
This matches what I needed
version.2.1.4.zip