Davuz Davuz - 7 months ago 35
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

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

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

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


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

Answer

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

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