From: daz Date: 2005-08-18T16:56:15+09:00 Subject: Re: Novice Q: What's the difference between /\s*/ and /(\s)*/? Mike Meng wrote: > # code > line = '/jazz/j00319.mp3 | 2:58 | Louis Armstrong | Wonderful World' > file, duration, artist, title = line.chomp.split(/\s*\|\s*/) > # code end > > [...] > While if I change the regex pattern in 'split' to /(\s)*\|(\s)*/, > [...] > What makes the differece? Any comments are appreciated. > Hi Mike, You're not seeing the difference because of your assignments. Try playing with this: #----------------------------------------------------------- def splt(patt) res = LINE.split(patt) print "#-> (%s)\n#-> %2d: " % [patt.inspect, res.size] res.to_a.each {|col| print ' (%s)' % [col]} puts; puts end LINE = 'ABC_:_KLM_:_NOP_:_XYZ' splt(/_:_/) splt(/(_:_)/) splt(/_(:)_/) splt(/(_):(_)/) splt(/((_):(_))/) splt(/((_)(:)(_))/) splt(/_:_K/) splt(/(_:_)K/) splt(/(_:_K)/) splt(/((_:_K))/) splt(/(((_:_K)))/) #----------------------------------------------------------- #-> (/_:_/) #-> 4: (ABC) (KLM) (NOP) (XYZ) #-> (/(_:_)/) #-> 7: (ABC) (_:_) (KLM) (_:_) (NOP) (_:_) (XYZ) #-> (/_(:)_/) #-> 7: (ABC) (:) (KLM) (:) (NOP) (:) (XYZ) #-> (/(_):(_)/) #-> 10: (ABC) (_) (_) (KLM) (_) (_) (NOP) (_) (_) (XYZ) #-> (/((_):(_))/) #-> 13: (ABC) (_:_) (_) (_) (KLM) (_:_) (_) (_) (NOP) (_:_) (_) (_) (XYZ) #-> (/((_)(:)(_))/) #-> 16: (ABC) (_:_) (_) (:) (_) (KLM) (_:_) (_) (:) (_) (NOP) (_:_) (_) (:) (_) (XYZ) #-> (/_:_K/) #-> 2: (ABC) (LM_:_NOP_:_XYZ) #-> (/(_:_)K/) #-> 3: (ABC) (_:_) (LM_:_NOP_:_XYZ) #-> (/(_:_K)/) #-> 3: (ABC) (_:_K) (LM_:_NOP_:_XYZ) #-> (/((_:_K))/) #-> 4: (ABC) (_:_K) (_:_K) (LM_:_NOP_:_XYZ) #-> (/(((_:_K)))/) #-> 5: (ABC) (_:_K) (_:_K) (_:_K) (LM_:_NOP_:_XYZ) daz