Visual Studio Code 中的正则表达式:无效匹配失败并显示“无法识别的标志";错误 [英] Regex in Visual Studio Code: Invalidate match fails with "Unrecognized flag" error

查看:100
本文介绍了Visual Studio Code 中的正则表达式:无效匹配失败并显示“无法识别的标志";错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如一些评论者指出的那样,它应该是 ^class (?!Migration)[A-Z][a-z]*.但是 VS Code 抱怨:Error parsing regex near "ss (?!Migr' at character offset 9: Unrecognized flag: '!'. (Allowed flag: i, m, s, U, u, x.)代码>

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?

解决方案

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.

这篇关于Visual Studio Code 中的正则表达式:无效匹配失败并显示“无法识别的标志";错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆