Davuz Davuz - 8 months ago 40
Python Question

Sublime Text 3 API plugin for regex replacing text

I'm writing a small plugin in Sublime Text 3 to replace all empty lines. I used

module to do regex replace text. These are my codes test on console:

>>> text = 'abc \n\nOk'
>>> print(text)

>>> text = re.sub(r'^\n','',text)
>>> text
'abc \n\nOk'

I can search on ST3 by
Ctrl+F = '^\n'
. Why does the pattern
not working in the plugin?


Because you didn't use multiline flag in your code. Try this:

re.sub(re.compile('^\n', re.MULTILINE), '', s)