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)
使用正则表达式?
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)
也许使用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)
| 归档时间: |
|
| 查看次数: |
1294 次 |
| 最近记录: |