Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 733
Davuz Avatar asked Dec 15 '25 03:12

Davuz


1 Answers

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

re.sub(re.compile('^\n', re.MULTILINE), '', s)
like image 87
Aminah Nuraini Avatar answered Dec 16 '25 17:12

Aminah Nuraini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!