GoldenAge
GoldenAge

Reputation: 3068

Visual Studio Code Regex contains but not ends with

I want to build regex expression in VS Code which returns all the phrases in the whole solution which contain a given string(please keep in mind that it can contain special characters) and not ends with given string e.g. Contains /webhelp but not ends with /

Matches:

/server/webhelp

blah/webhelp#

Doesn’t match

/server/webhelp/

server#webhelp/

Im not an expert in Regex, I’ve tried to build something like:

(?=/webhelp)(?=.*(?<!/)$)

But it doesn’t work.

Upvotes: 5

Views: 4892

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626738

You can use infinite-width lookahead and lookbehind without any constraint beginning with Visual Studio Code v.1.31.0 release, and you do not need to set any options for that now.

With earlier versions, you may use lookaheads. The regex that should match what you need is

/webhelp(?!.*/$).*$

enter image description here

Details

  • /webhelp - a literal substring
  • (?!.*/$) - a negative lookahead that makes sure the line does not end with /
  • .*$ - the rest of the line.

It still works even in Find in files:

enter image description here

Upvotes: 4

Poul Bak
Poul Bak

Reputation: 10929

Here's a slightly shorter version of the regex:

/webhelp(?!/)

It simply matches '/webhelp' unless it's followed by a slash.

Upvotes: 5

Related Questions