我在Sublime Text 3中编写了一个小插件来替换所有空行。我用re
模块来做正则表达式替换文本。这些是我在控制台上测试的代码:
>>> text = 'abc \n\nOk'
>>> print(text)
abc
Ok
>>> text = re.sub(r'^\n','',text)
>>> text
'abc \n\nOk'
我可以在上搜索ST3 Ctrl+F = '^\n'
。为什么该模式^\n
在插件中不起作用?
因为您没有在代码中使用多行标志。试试这个:
re.sub(re.compile('^\n', re.MULTILINE), '', s)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句