views:

53

answers:

1

How can one change the following text

The quick brown fox jumps over the lazy dog.

to

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

using regex?

UPDATE1

A solution for Ruby is still missing... A simple one I came to so far is

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.
+4  A: 

Maybe use textwrap instead of regex:

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)))

yields:

The quick brown fox + 
    jumps over the + 
    lazy dog.
unutbu
Thanks! However, I also need it for Ruby for which I cannot find such function. It seems that I need to write it myself...
Andrey