当你还需要缩进包裹的行时,如何使用正则表达式在文本中包装长行?

And*_*rei 0 ruby python regex word-wrap

如何更改以下文本

The quick brown fox jumps over the lazy dog.
Run Code Online (Sandbox Code Playgroud)

The quick brown fox +
    jumps over the +
    lazy dog.
Run Code Online (Sandbox Code Playgroud)

使用正则表达式?

UPDATE1

Ruby的解决方案仍然缺失...到目前为止我遇到的一个简单的解决方案是

def textwrap text, width, indent="\n"
  return text.split("\n").collect do |line|
    line.scan( /(.{1,#{width}})(\s+|$)/ ).collect{|a|a[0]}.join  indent
  end.join("\n")
end
puts textwrap 'The quick brown fox jumps over the lazy dog.', width=19, indent=" + \n    "
# >> The quick brown fox + 
# >>     jumps over the lazy + 
# >>     dog.
Run Code Online (Sandbox Code Playgroud)

unu*_*tbu 5

也许使用textwrap而不是正则表达式:

import textwrap

text='The quick brown fox jumps over the lazy dog.'

print(' + \n'.join(
    textwrap.wrap(text, initial_indent='', subsequent_indent=' '*4, width=20)))
Run Code Online (Sandbox Code Playgroud)

收益率:

The quick brown fox + 
    jumps over the + 
    lazy dog.
Run Code Online (Sandbox Code Playgroud)