Sublime Forum

[Solved] View.find_all Doesn't Work With Certain Characters

#1

I’m work on making my own version of the find_under_expand command that supports some extra features that I would like. I’m using the self.view.find_all command in a TextCommand plugin of my own making. This works great except for when the regular expression contains one of the following escaped characters:

` ’ < >

For example the following regular expression returns no results from a file that clearly should have matches:

variable\-\>item

Sublime also freezes (probably just looping forever somewhere) if the regular expression only contains one of those characters. For example, the following regular expression makes find_all never return:

\`

Let me know if I’m doing something wrong with the expression or the use of find_all. The code for my custom plugin can be found at: https://bitbucket.org/stampede247/sublimedata/src/201bc76af37ffbd18e78790a6eac73dee75adfcc/Packages/User/GrabNext.py

0 Likes

#2

it looks like the problem is that you are using Python’s regular expression library to do the escaping, and the Boost regex engine that ST uses treats some of those escaped characters as meta characters.

for example, for some reason Python’s re module is escaping characters that don’t need to be escaped:

>>> import re
>>> re.escape(r'`><')
'\\`\\>\\<'

which, as has been discovered before, \> and \< has special meaning in Boost

probably the workaround is not to use a handy prebuilt method to escape the input text (ST doesn’t offer one for Boost), but escape it yourself…

2 Likes

#3

Thanks for the reply. Sorry for re-asking the question. For now I am manually undoing the escaped characters since that seems easier than trying to do the escaping myself. Here’s the code I have if anyone is interested.

regexStr = re.escape(lastRegionStr)
regexStr = regexStr.replace("\\<", "<")
regexStr = regexStr.replace("\\>", ">")
regexStr = regexStr.replace("\\'", "'")
regexStr = regexStr.replace("\\`", "`")
1 Like