带有负向后视的 VSCode 内部正则表达式搜索 [英] VSCode internal regex search with negative lookbehind

查看:26
本文介绍了带有负向后视的 VSCode 内部正则表达式搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 VSCode 中,尝试搜索 print(print ( - 但前提是后面没有 #

In VSCode, attempting to search for print( and print ( - but only if not followed by #

这是我第一次尝试在 VSCode 中进行正则表达式搜索...

示例:
print ('Test One') - MATCH
print('测试二') - MATCH
#print('Test Fee') - 跳过

Examples:
print ('Test One') - MATCH
print( 'Test Two') - MATCH
#print('Test Fee') - SKIPPED

我从 这个问题 了解到 VSCode 缺乏负向后视.

I understand from this question that VSCode lacks negative lookbehind.

通常,在 ^F(搜索功能)中,我会使用类似(未经测试)的内容:

Ordinarily, in the ^F (search function) I would use something like (untested):

/w*(?<!#)print

但我收到正则表达式无效的错误.

but I am getting the error that the regex is invalid.

任何人都可以提出解决方法 - 或者我只是对正则表达式进行了胖手指?

Can anyone suggest a workaround - or have I just fat-fingered the regex ?

推荐答案

更新注意:从 VS Code 1.31 开始,支持无限宽度的后视.

UPDATE NOTE: Starting with VS Code 1.31, infinite-width lookbehinds are supported.

但是,在当前场景中,您不必使用基于后视的解决方案,您可以使用

However, in the current scenario, you do not have to use a lookbehind based solution, you may use

^\s*print\s*\(

查看正则表达式演示

请注意,如果您只想匹配同一行上的文本,最好将 \s 替换为 [ \t],或者 <代码>[^\S\n].

Note that in case you only want to match text on the same lines, it may be a better idea to replace \s with [ \t], or [^\S\n].

详情

  • ^ - 一行的开始
  • \s* - 0+ 个空格
  • print - 文字子串
  • \s* - 0+ 个空格
  • \( - ( 字符(必须转义以匹配文字 ()).
  • ^ - start of a line
  • \s* - 0+ whitespaces
  • print - a literal substring
  • \s* - 0+ whitespaces
  • \( - a ( char (must be escaped to match a literal ().

注意,实际上 VSCode 仍然支持前瞻,但您需要启用search.usePCRE2 选项.

NOTE that actually VSCode still supports lookaheads, but you need to enable search.usePCRE2 option.

这篇关于带有负向后视的 VSCode 内部正则表达式搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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