On Jul 20, 2007, at 3:15 AM, Michael W. Ryder wrote:

> Morton Goldberg wrote:
>> How about this?
>> n = 3
>> "a b c d e f".sub(/(\S\s){#{n}}/) { |m| m.delete(" ") } # => "abcd  
>> e f"
>> n = 10
>> "a b c d e f".sub(/(\S\s){#{n}}/) { |m| m.delete(" ") } # => "a b  
>> c d e f"
>> Regards, Morton
>
> Is there nothing in regular expressions where you can tell it to do  
> something up to n times?

OK, is this what you're looking for?

result = []
7.times do |n|
    result << "a b c d e f".sub(/(\S\s){0,#{n}}/) { |m| m.delete(" ") }
end
result # =>
    ["a b c d e f", "ab c d e f", "abc d e f", "abcd e f", "abcde f",  
"abcdef", "abcdef"]

Regards, Morton