gpt4 book ai didi

regex - Visual Studio代码: regex expression starts with but not ends with

转载 作者:行者123 更新时间:2023-12-01 14:37:33 31 4
gpt4 key购买 nike

Visual Studio Code 使用 ripgrep to search in files ,其中 doesn't support look around and backreferences .

我想构建一个表达式,它返回以给定字符串开头且不以给定字符串结尾的所有短语,例如:

以“http://blah.com”开头,但不以“.htm”结尾

匹配:

http://blah.com/tmp

不匹配:

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

最佳答案

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

 "search.usePCRE2": true

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

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

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

来自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.

Positive and negative lookaheadsbackreferences .

另请注意,以前的解决方案已被弃用:

// Deprecated. Consider "search.usePCRE2" for advanced regex feature support.
// This setting is deprecated and now falls back on "search.usePCRE2".

"search.useRipgrep": false

关于regex - Visual Studio代码: regex expression starts with but not ends with,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53375311/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com