2

Visual Studio Code uses ripgrep to search in files, which doesn't support look around and backreferences.

I want to build an expression which returns all the phrases which start with a given string and does not end with given string e.g:

Starts with "http://blah.com" but not ends with ".htm"

Matches:

http://blah.com/tmp

Doesn’t match:

http://blah.com/tmp.htm
blah.com/tmp.htm
Mark
  • 143,421
  • 24
  • 428
  • 436
GoldenAge
  • 2,918
  • 5
  • 25
  • 63

1 Answers1

5

In your settings (to enable lookahead) :

 "search.usePCRE2": true

Your regex (using a negative lookahead):

blah\.com(?!.*\.htm$)

[Edit] Added the $ if you absolutely want to exclude matches that do not end with the .htm, not just followed somewhere in the string by .htm.

From the v1.29 release notes:

It is also now possible to use backreferences and lookahead assertions in regex searches, by setting "search.usePCRE2": true. 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.

Positive and negative lookaheads and backreferences.

Also note that a previous solution for this has been deprecated:

// Deprecated. Consider "search.usePCRE2" for advanced regex feature support.
// This setting is deprecated and now falls back on "search.usePCRE2".

"search.useRipgrep": false
Mark
  • 143,421
  • 24
  • 428
  • 436