From: Robert Klemme Date: 2007-08-03T22:49:47+09:00 Subject: Re: Determining the common prefix for several strings ------=_Part_89784_24707089.1186148990033 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 2007/8/3, Daniel DeLorme : > Robert Klemme wrote: > > I believe you are wrong. Look at "\A(.*).*" - there are multiple ways > > this can match (including matching \n inside any of the .* parts). > > Anchoring does not help much here IMHO. > > Yes, there is backtracking for the first part (there has to be, the > whole point being to find the longest matching string), but \n CANNOT > match inside any of the .* parts unless you specify the m modifier. Correct. I always confuse Ruby's /m with other language's variants. Thanks for the reeducation! Still it would be interesting to do benchmarks with larger numbers of words to see what happens.... I have attached a test. It seems the RX approach is faster very long. Kind regards robert ------=_Part_89784_24707089.1186148990033 Content-Type: application/x-ruby; name="pref-rb.rb" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="pref-rb.rb" X-Attachment-Id: f_f4wqh66b cmVxdWlyZSAnYmVuY2htYXJrJwoKTElTVCA9IDw8LUVPUy50b19hLmZyZWV6ZQppdGVtMDAxCml0 ZW0wMDEKaXRlbTAwNAppdGVtMDAyCml0ZW0wMDJiCml0ZW0wMDJDCml0ZW0gNQppdGVtIDEwCml0 ZW1pemUgdGhpcwpFT1MKCkJlbmNobWFyay5ibSgxNSkgZG8gfHh8CiAgaXQgPSAyCiAgbG9vcCBk bwogICAgbCA9IExJU1QuZHVwCiAgICBpdC50aW1lcyB7bC5jb25jYXQgbH0KICAgIGl0ICs9IDEK CiAgICBsMSA9IGwuam9pbgogICAgbDIgPSBsLmR1cAoKICAgIHgucmVwb3J0KCJyeCAgICU1ZCIg JSBsLnNpemUpIGRvCiAgICAgIHByZWYgPSAvXEEoLiopLiooXG5cMS4qKSpcWi8ubWF0Y2gobDEp WzFdCiAgICAgIHJhaXNlICJlcnJvciA8I3twcmVmfT4iIHVubGVzcyAiaXRlbSIgPT0gcHJlZgog ICAgZW5kCiAgICAKICAgIHgucmVwb3J0KCJpdGVyICU1ZCIgJSBsLnNpemUpIGRvCiAgICAgIHBy ZWYgPSBsMi5zaGlmdAogICAgICBsMi5lYWNoIGRvIHxzfAogICAgICAgIGkgPSAwCiAgICAgICAg d2hpbGUgcHJlZltpXSA9PSBzW2ldICYmIGkgPCBwcmVmLnNpemUKICAgICAgICAgIGkgKz0gMQog ICAgICAgIGVuZAogICAgICAgIHByZWYuc2xpY2UhIGkuLi0xCiAgICAgIGVuZAogICAgICByYWlz ZSAiZXJyb3IgI3twcmVmfSIgdW5sZXNzICJpdGVtIiA9PSBwcmVmCiAgICBlbmQKICBlbmQKZW5k Cg== ------=_Part_89784_24707089.1186148990033--