Home Download Buy Blog Forum Support

view.replace() regex

Re: view.replace() regex

Postby facelessuser on Wed May 22, 2013 7:22 pm

robertcollier4 wrote:Thanks bizoo... adding the r"" to indicate raw string did the trick.

It is in fact possible to do regex-replace via sublime API with the "format string" and "extractions list". The following code works as an example of how to do a regex-replace from Sublime API.

Code: Select all
import sublime, sublime_plugin

class PathUncToLocalRegexReplaceExampleCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      ListReplacements = []
      RegionsResult = self.view.find_all(r"\\\\(\S)*\\([A-Z])\$", sublime.IGNORECASE, "\\2:", ListReplacements)
      for i, thisregion in reversed(list(enumerate(RegionsResult))):
        self.view.replace(edit, thisregion, ListReplacements[i])

Above example successfully replaced:

I used to use the API to do regex replaces, but the regex engine is different from the Python one, and I found it wasn't as flexible as the Python one, but yes you can use extractions in the API which is what I used to do up until recently. I found using Python's re module and reading the view buffer to be easier when doing complex stuff.
Posts: 1780
Joined: Tue Apr 05, 2011 7:38 pm

Re: view.replace() regex

Postby sublimator on Fri May 24, 2013 6:43 pm

It's not all that bad. You can embed flags in the sublime style regex:


so `.*` will match new lines

Pretty sure it uses the the perl variant of the boost regex lib
It is better to remain silent and be thought a fool, than to speak out and remove all doubt
Posts: 645
Joined: Thu Mar 20, 2008 5:41 am


Return to Plugin Development

Who is online

Users browsing this forum: Google [Bot] and 3 guests