Topic: Reg Exp question

This is probably quite easy, but I really suck at regular expressions and can't seem to get this right...

I want to split a string into substrings. The string should be split into n equally long parts, where n is the squareroot of the length of the string. A 9-char string would then be 3 parts, a 16-char string 4 and so on... Like this:

str="abcdefghi"
str.split( a_nifty_regexp_here ) => ["abc","def","ghi"]

str2="abcdefghijklmnop"
str2.split( a_nifty_regexp_here ) => ["abcd","efgh","ijkl","mnop"]

I really wish I can take the time someday and properly learned some regexp...

Cheers,
Peter

Last edited by Peter (2007-03-01 08:46:56)

Re: Reg Exp question

Try  this:

str.scan(/.{3}/)

Then you just need to replace 3 with the number you want:

str.scan(/.{#{ceil(n/n)}}/)

I think that will work anyway, untested.

Railscasts - Free Ruby on Rails Screencasts