4

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

This is my first time attempting a regex search in VSCode...

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

I understand from this question that VSCode lacks negative lookbehind.

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 ?

crashwap
  • 2,846
  • 3
  • 28
  • 62

2 Answers2

6

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*\(

See the regex demo

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

Details

  • ^ - start of a line
  • \s* - 0+ whitespaces
  • print - a literal substring
  • \s* - 0+ whitespaces
  • \( - a ( char (must be escaped to match a literal ().

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

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

for negative lookaheads you can use this VS code Extension Reverse Search

Danish Sarwar
  • 343
  • 2
  • 8