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
(?=^\s*)print_debug
positive lookahead is not matching the
\s*
spaces?



The full regex pattern is
(?=^\s*)print_debug
. 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
else:
print_debug('SKIPPED...')


However it is only matching the first two lines:

Only matching the first two lines

Here is the link to a online regex engine:


  1. https://regex101.com/r/RIIqTg/1






Why the
\s*
are being ignored when used with
^
?




  1. If you remove the
    ^
    the
    \s*
    start matching.

  2. If you add the
    ^
    , the
    \s*
    stop working.



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

Answer

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

Solution

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

^\s*\Kprint_debug

See the regex demo

Details:

  • ^ - 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

Comments