Reputation: 3068
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
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(?!.*/$).*$
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:
Upvotes: 4
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