What I *want to* do (nobody asks me): take a string of arbitrary
length, or an array of such random strings and modify them in a way
that, in the result, all strings are split up into chunks of a maximum string-length.
-----------
me@here:/tmp$ ./test.rb "Ein König mit Krone ist besser als ohne" 15
Ein König mit
Krone ist
besser als
ohne
-----------
"Ein König mit Krone ist besser als ohne"irb(main):015:0> str.scan %r{\S.{0,#{max_len - 1}}(?!\S)}
["Ein König mit", "Krone ist", "besser als ohne"]
On 19.12.2016 17:19, Michael Uplawski wrote:
max_len = 10
strings = str.scan /.{1,#{max_len}}/
It seems your last two strings should be one.
Cheers
robert
Good morning.
On Tue, 20 Dec 2016 19:16:52 +0100,
Robert Klemme <shortcutter@googlemail.com> wrote:
On 19.12.2016 17:19, Michael Uplawski wrote:
max_len = 10
strings = str.scan /.{1,#{max_len}}/
Thank you. This is clearly the function that I need.
I am not jubilating for the simple reason, that I understand the documentation on String.scan only with this concrete example and already know, that I cannot use the function for anything that I have not yet
seen in code.
It solves my current problem, though.
Nice holidays all, happy new year, too, within the limits of the
televised joy.
You should really get familiar with regular expressions. One book I
usually recommend is "Mastering Regular Expressions" (O'Reilly).
There is also a nice programm called "Regex Coach" which allows to watch
a regex engine match. This is really helpful for learning. Runs under
WINE as well.
http://weitz.de/regex-coach/
It solves my current problem, though.
But note the form above does _not_ do word splitting. For that you need
the other one.
Nice holidays all, happy new year, too, within the limits of the
televised joy.
Same to you!
Kind regards
robert
On Wed, 21 Dec 2016 19:02:29 +0100,
Robert Klemme <shortcutter@googlemail.com> wrote:
You should really get familiar with regular expressions. One book I
usually recommend is "Mastering Regular Expressions" (O'Reilly).
You are probably right. The difficulties that I have with String.scan
(and functions documented in a similar way) are certainly induced by my
using Regexps scarcely and only those, that I already master for some
time.
In which way do you consider the O'Reilly book superior to other
publications or, to simplify, the man-pages to egrep, regex (7) or the
RDoc to Regexp, my principal though rarely consulted sources of
information?
I will consider this. Paul Lutus put a “Regular Expression Laboratory”
on his web-site (what did he *not* put on his web-site, actually?). It
is simpler with less options than the “Regex-Coach”: http://arachnoid.com/regex_lab/index.html
Cheerio.
arr = str.split(/\s/)
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 296 |
Nodes: | 16 (2 / 14) |
Uptime: | 54:07:08 |
Calls: | 6,650 |
Calls today: | 2 |
Files: | 12,200 |
Messages: | 5,330,613 |