我想用每个空格的nbsp;
替换前导空格。在
所以:
spam --> spam
eggs --> eggs
spam eggs --> spam eggs
我见过一些使用regex的解决方案,但都是用其他语言编写的。
我在Python中尝试了以下方法,但没有成功。在
^{pr2}$
最后一行似乎最近,但没有雪茄。在
在Python中,用
替换每个前导空格的正确方法是什么?在
如果没有正则表达式,有一个干净的方法可以做到这一点,我很乐意接受,但我自己无法解决。在
Tags:
使用regex模块(在注释中由Wiktor Stribiżew回答)
根据文档:
可以将^{} 与回调函数一起使用,并计算匹配的长度:
在这里,您甚至不需要昂贵的regex,只需去掉前导空格,并为剥离的字符数准备一些
个字符:相关问题 更多 >
编程相关推荐