Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

Regex in Visual Studio Code: Invalidate match fails with "Unrecognized flag" error

Edit: As several commenters have pointed out, it should be ^class (?!Migration)[A-Z][a-z]*. But VS Code complains: Error parsing regex near "ss (?!Migr' at character offset 9: Unrecognized flag: '!'. (Allowed flags: i, m, s, U, u, x.)

Visual Studio seems to support negative look aheads (see "Invalidate a match") . Could not find an answer to the question if VSCode does support it.


In VS Code I am trying to find class definitions in a Django project via Shift+Command+F. Turned on regex search. Now my search pattern is this:

^class [A-Z][a-z]*

So every occurence of "class Abc", where Abc is the class name, will be found.

Now I would like to exclude classes like class Migration(...) or class Command(...)

I tried to do this with a negative lookahead, like so:

^class (!?Migration)[A-Z][a-z]*

But that won't work. VS Code will show me all classes named class Migration(...), so instead of excluding it, it will focus on it.

What am I missing?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

UPDATE:

You may use lookarounds freely in your regexps, both lookaheads and lookbehinds, as a result of moving to Electron 3.0. Since lookarounds are supported since Chromium 62 and Node 8.10.0, and Electron 3.0 uses Chromium 66 and Node 10.2.0, they are now supported out-of-the-box.

PCRE2 option is

Deprecated. PCRE2 will be used automatically when using regex features that are only supported by PCRE2.

Note that starting with VS Code 1.31, even infinite-width lookbehinds are supported.

Previous answer for legacy versions

Note that to make your patterns with lookaheads work you need to edit your settings.json file to set

"search.usePCRE2": true

See 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.

Also, see Mark's answer who noticed this option earlier.

Then, your ^class (?!Migration)[A-Z][a-z]* regex will work.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...