Sublime Forum

Find_all unexpectedly finds 0 width regions

#1

I am trying to find some operators and remove the whitespace around them.
For some reason if I include <, <<, > or >> operators, I get back zero width regions.
Can anyone spot what is wrong?

import sublime
import sublime_plugin

import re
import pprint

class TestCommand(sublime_plugin.TextCommand):
    def run(self, edit):

        operatorList = [
            "\\",
            r"/",
            r"!",
            r"=",
            r"+",
            r"*",
            # r"<", # Causes 0 width region
            # r">", # Causes 0 width region
            # r"<<", # Causes 0 width region
            # r">>", # Causes 0 width region
            r"!=",
            r"==",
        ]

        alternation = '|'.join([re.escape(operator) for operator in sorted(operatorList, key=len, reverse=True)])
        regex =  r'\s*({})\s*'.format(alternation)

        extractions = list()
        regions = self.view.find_all(regex, 0, '$1', extractions)
        replacements = list(zip(regions, extractions))

        pp = pprint.PrettyPrinter(indent=4)
        pp.pprint(replacements)

Also, wondering if there is a way to exclude operators that don’t have any whitespace before or after them. I’m using \s* on either side as one side may not have whitespace while the other does.

0 Likes

#2

you’re using Python’sre module to escape a regex pattern for a Boost engine (iirc), which treats \> etc as a word boundary

3 Likes

#3

Yes, that’s it.
Thanks

0 Likes