问题描述
在 VSCode 中,尝试搜索 print(
和 print (
- 但前提是后面没有 #
In VSCode, attempting to search for print(
and print (
- but only if not followed by #
这是我第一次尝试在 VSCode 中进行正则表达式搜索...
示例:print ('Test One')
- MATCHprint('测试二')
- MATCH#print('Test Fee')
- 跳过
Examples:print ('Test One')
- MATCHprint( '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+ whitespacesprint
- 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 内部正则表达式搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!