WoNáDo
6/30/2006 11:16:00 AM
> Well, what do you expect with a such regexp ?
Very simple - I'm writing examples for some information texts about "RegExes
in Ruby", this part is related to "look-behind" in Ruby 1.9.
The construct was planned inside a larger Rexexp that makes simple text
processing. The goal was to produce from
"Liebe Rubyistinnen und Rubyisten! Anlaesslich der immer wieder, ja taeglich
anzutreffenden ausserordentlichen
Freude, die Ihr, liebe Rubyistinnen und Rubyisten, bei der Benutzung unserer
ausserordentlichen Lieblingssprache
Ruby empfinden duerft, haben wir Euch alle hier zum Bankett der Rubyistinnen
und Rubyisten geladen. Benutzt die
Angebote des Bueffets genau so, wie Ihr Ruby nutzt: Einfach nach Wunsch
zusammenstellen und geniessen."
the output
"Liebe Rubyistinnen und Rubyisten!
Anlaesslich der immer wieder, ja
taeglich anzutreffenden
ausserordentlichen Freude, die Ihr,
liebe Rubyistinnen und Rubyisten, bei
der Benutzung unserer ausserordentlichen
Lieblingssprache Ruby empfinden duerft,
haben wir Euch alle hier zum Bankett der
Rubyistinnen und Rubyisten geladen.
Benutzt die Angebote des Bueffets genau
so, wie Ihr Ruby nutzt: Einfach nach
Wunsch zusammenstellen und geniessen."
To come to this result, the most simple solution is based on the pattern
/(((\w+[:.,;?!]? )+)(?=.*(?=\G.{41}))|(\w+[:.,;?!]? ))/
which should be used as argument for "String#scan" on a preprocessed input
(one line, blank after each token).
Unfortunately it does not work (error message, that "\G" is not allowed in
look-behind). I changed the expression to
/(((\w+[:.,;?!]? )+)(?=.*(?=^.{41}))|(\w+[:.,;?!]? ))/
and used "String#sub!" inside a while loop, which works! I'm not happy with
this solution, because it is artificial (extra lines of code, and
destructive on input).
I don't understand the reason for it, because "\G.{41}" is of fixes size and
anchored similar to "^.{41}". If it is a bug - O.K., fine to find it, if it
is a feature, I don't understand the reason to be one.