Python 当您还需要缩进已换行的行时,如何使用正则表达式在文本中换行?

Python 当您还需要缩进已换行的行时,如何使用正则表达式在文本中换行?,python,ruby,regex,word-wrap,Python,Ruby,Regex,Word Wrap,如何更改以下文本 The quick brown fox jumps over the lazy dog. 到 使用正则表达式 更新1 Ruby的解决方案仍然缺失。。。到目前为止,我想到的一个简单的例子是 def textwrap text, width, indent="\n" return text.split("\n").collect do |line| line.scan( /(.{1,#{width}})(\s+|$)/ ).collect{|a|a[0]}.join

如何更改以下文本

The quick brown fox jumps over the lazy dog.

使用正则表达式

更新1 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.
可能使用以下替代正则表达式:

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)))
收益率:

The quick brown fox + 
    jumps over the + 
    lazy dog.
可能使用以下替代正则表达式:

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)))
收益率:

The quick brown fox + 
    jumps over the + 
    lazy dog.

谢谢然而,对于Ruby,我也需要它,因为我找不到这样的函数。看来我需要自己写…谢谢!然而,对于Ruby,我也需要它,因为我找不到这样的函数。似乎我需要自己编写它……使用/x修饰符的Perl?;)带/x修饰符的Perl?;)