Davuz Davuz - 2 years ago 123
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?

Answer Source

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

re.sub(re.compile('^\n', re.MULTILINE), '', s)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download