From: Victor Reyes Date: 2008-06-02T20:30:32+09:00 Subject: Re: Remove Parts of a String ------=_Part_5027_21314656.1212406261760 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Mon, Jun 2, 2008 at 2:20 AM, Stefano Crocco wrote: > 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 > > Stefano, Thank you for the quick explanation. It really helps. The link was also very helpful. It is like everything else in life. Once you understanding it the mystique is gone! Thanks again, Victor ------=_Part_5027_21314656.1212406261760--