From: Alan Burch Date: 2006-03-12T00:44:17+09:00 Subject: Re: Help me understand why the Ruby block is slower than wit Benjohn Barnes wrote: > On 11 Mar 2006, at 02:13, George Ogata wrote: > >> Another speedup: replace: >> >> w.split(//).uniq.size == 11 >> >> with: >> >> w !~ /(.).*\1/ > > !? :) How on earth does that work? Every time I think I've sort of > got the hang of regexp, they spring something new on me. > > I was also going to ask why everyone was doing "split( // )" instead > of "split( '' )"? > > - oooh, coffee's ready... > > Cheers, > Benjohn Benjohn: A . matches any char except the \n, putting it in (), makes it save in \1 each time it matches, the .* matches zero or more chars that's not a \n, so if we try to match a string such as "profligate\n" the regex would first look for (p).*\p, with the second p being anywhere in the string then (r).*(r), etc. A string with a repeating set of letters "wristwatch\n" matches (w)rist\w and returns a match. I highly recommend O'reilly's "Mastering Regular Expressions", I've only read the first edition, but it's an eye-opener (or maybe the opposite if you try to read it in bed :)) A note for those following along in the DOS world, the dos string ends \r\n and won't return the expected result as a matching DOS string will need to be 12 long. This sacrifices portability for speed (I didn't want to use chop after each gets). As to split, I just used what I'm used to from perl. It's an empty pattern and makes sense to me that way. Alan -- Posted via http://www.ruby-forum.com/.