Visual Studio Code:正则表达式以开头但不结尾 [英] Visual Studio Code: regex expression starts with but not ends with

查看:40
本文介绍了Visual Studio Code:正则表达式以开头但不结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual Studio Code 使用 ripgrep 搜索文件,其中不支持环顾和反向引用.

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:

http://blah.com"开头但不以.htm"结尾

匹配:

http://blah.com/tmp

不匹配:

http://blah.com/tmp.htm
blah.com/tmp.htm

推荐答案

在您的设置中(启用前瞻):

In your settings (to enable lookahead) :

 "search.usePCRE2": true

您的正则表达式(使用负前瞻):

Your regex (using a negative lookahead):

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

添加 $ 如果您绝对想排除不以 .htm 结尾的匹配项,而不只是在字符串中的某处跟随 .htm.

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.

来自 v1.29 发行说明:

现在也可以使用反向引用和前瞻断言在正则表达式搜索中,通过设置search.usePCRE2":true.这配置ripgrep 使用 PCRE2 正则表达式引擎.虽然 PCRE2 支持许多其他功能,我们只支持仍然有效的正则表达式JavaScript,因为仍然使用编辑器搜索打开的编辑器基于 JavaScript 的搜索.

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

这篇关于Visual Studio Code:正则表达式以开头但不结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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