有什么理由不只用不间断空格替换所有空格?( r/ / /
.)
It won't change the appearance of normal English text (except where the source had extraeneous double-spaces) and your code blocks will render correctly.
For fun, my attempt in Python:
>>> eight_spaces = " hello world!"
>>> re.sub(r"^(|(?: )*)\s",r"\1 ",eight_spaces)
' hello world!'
The idea is to replace one space at a time. It doesn't work because the re
engine doesn't go back to the start of the line after a match - it consumes the string working left to right.
Note the alternation of (?: )*
with the empty string, (|(?: )*)
, so that the capture group \1
always captures something (even the empty string.)