Home Download Buy Blog Forum Support

Help with programmatic search replace

Help with programmatic search replace

Postby jktravis on Sat Mar 09, 2013 1:14 am

Hi all,

I've made a simple plugin that updates meta data in an HTML page on command. The problem is when a closing slash is included in the tag, the search doesn't match it. When I copy and paste the regex into the editor directly, it matches perfectly. I don't know if this is a regex problem (with the trailing slash being interpreted as a segmentation character), if this is a view.find() problem, both, or something else entirely. The relevant part of my code is below:

Code: Select all
import sublime, sublime_plugin, os

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

# Get the current user environment variable. Windows uses 'USERNAME', while Mac uses 'USER'
      user = os.environ.get( "USERNAME" )
      if not user:
         user = os.environ.get( "USER" )
#Searches and replaces the developer tag;
#Regex here matches in regular find/replace with regex turned on.
#In code, it matches:
#<meta name="developer" content="foo">
#but not
#<meta name="developer" content="foo"/>
#<meta name="developer" content="foo" />
      tag = self.view.find('<meta name="developer" content=".[^>]*>', sublime.IGNORECASE)
      self.view.replace(edit, tag, '<meta name="developer" content="' + user + '">')

I've tried a couple variations of the regex to no avail. The above and below:

Code: Select all
<meta name="developer" content=".*

The console gives me an error that seems to be related to the fact that tag is ending up null because view.find() isn't matching anything.

Boost.Python.ArgumentError: Python argument types in
View.replace(View, Edit, NoneType, str)
did not match C++ signature:
replace(SP<TextBufferView>, SP<Edit>, SelectionRegion, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >)

I'm fairly new to plugin (and Python for that matter) development, so I apologize in advance if this is documented someplace. Any help would be appreciated.
Posts: 5
Joined: Sat Oct 27, 2012 1:46 am

Re: Help with programmatic search replace

Postby sapphirehamster on Sat Mar 09, 2013 7:22 pm

You might want to check, find() takes 3 arguments. The second argument is the "from position", like 0.
Posts: 95
Joined: Sun Jul 01, 2012 11:19 pm

Re: Help with programmatic search replace

Postby jktravis on Mon Mar 11, 2013 7:15 pm

Bah. Thanks for the help. That seemed to have fixed it!
Posts: 5
Joined: Sat Oct 27, 2012 1:46 am

Return to Plugin Development

Who is online

Users browsing this forum: Exabot [Bot] and 4 guests