From: Stefano Crocco Date: 2008-06-02T15:20:11+09:00 Subject: Re: Remove Parts of a String On Monday 02 June 2008, Victor Reyes wrote: > > already spoke, after all, I am just learning Ruby. However, I do find that > to understand: > > str.gsub(/:[^,]*(?:$|(?=,))/,'').split( ',') > > you need a PhD in "General Expresionology". Ruby is supposed to be simple. > Here you have 1 line of code but need 1 page to explain it. I do find it > elegant, although I don't understand it. I only wish I could do that to! > > Victor To understand it, you need to know regexps. The first argument of gsub is a regexp which matches all substrings starting with a colon, followed by any number of character except comma followed by a comma (which is not included in the match, which explains the use of the (?=,) construct). The second argument of gsub tells that the parts of the string matching the previous regexp should be replaced with an empty string. The returned string is then split at commas. If you want a quick reference on regexps in ruby, you can look here: http://www.zenspider.com/Languages/Ruby/QuickRef.html#11. If you need a more complete introduction, there's a long description in the free edition of the Pickaxe: http://www.ruby-doc.org/docs/ProgrammingRuby/html/intro.html#S5 I hope this helps Stefano