addons_zz addons_zz - 10 days ago 6
Python Question

Why this (?=^\s*)print_debug positive lookahead is not matching the \s* spaces?

Why this
positive lookahead is not matching the

The full regex pattern is
. And the sample matching is:

print_debug('scroll set: '+str(position));
print_debug('scroll set: '+str(position));
print_debug('scroll set: '+str(position));
print_debug('supposed current scroll: '+str(view.viewport_position())); # THIS LIES

However it is only matching the first two lines:

Only matching the first two lines

Here is the link to a online regex engine:


Why the
are being ignored when used with

  1. If you remove the
    start matching.

  2. If you add the
    , the
    stop working.

I want it to use on
Sublime Text
and disable all debugging statements from my
source code files for the Sublime Text packages.


Root cause

The (?=^\s*) is a positive lookahead that matches the start of a line (in SublimeText, the ^ matches the line start by default), and then 0+ whitespaces (i.e. the \s* does not have to match any text at all!).

Thus, your (?=^\s*)print_debug pattern matches print_debug if it is at the start of the string (note the (?!^) works the same way as (?<!^) as the ^ is a zero-width assertion).


Since there is no variable-width lookbehind support in SublimeText regex, you can leverage the \K match reset operator:


See the regex demo


  • ^ - start of a line
  • \s* - zero or more whitespaces (may be replaced with \h* to only match horizontal whitespace)
  • \K - omitting the whole text matched so far
  • print_debug - a literal string.

enter image description here